Class yii\console\Application
Inheritance | yii\console\Application » yii\base\Application » yii\base\Module » yii\di\ServiceLocator |
---|---|
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/console/Application.php |
Application represents a console application.
Application extends from yii\base\Application by providing functionalities that are specific to console requests. In particular, it deals with console requests through a command-based approach:
- A console application consists of one or several possible user commands;
- Each user command is implemented as a class extending yii\console\Controller;
- User specifies which command to run on the command line;
- The command processes the user request with the specified parameters.
The command classes should be under the namespace specified by $controllerNamespace.
Their naming should follow the same naming convention as controllers. For example, the help
command
is implemented using the HelpController
class.
To run the console application, enter the following on the command line:
yii <route> [--param1=value1 --param2 ...]
where <route>
refers to a controller route in the form of ModuleID/ControllerID/ActionID
(e.g. sitemap/create
), and param1
, param2
refers to a set of named parameters that
will be used to initialize the controller action (e.g. --since=0
specifies a since
parameter
whose value is 0 and a corresponding $since
parameter is passed to the action method).
A help
command is provided by default, which lists available commands and shows their usage.
To use this command, simply type:
yii help
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$bootstrap | array | List of components that should be run during the application bootstrapping process. | yii\base\Application |
$charset | string | The charset currently used for the application. | yii\base\Application |
$controller | yii\console\Controller | The currently active controller instance | yii\console\Application |
$controllerMap | array | Mapping from controller ID to controller configurations. | yii\base\Module |
$controllerNamespace | string | The namespace that controller classes are located in. | yii\base\Application |
$defaultRoute | string | The default route of this application. | yii\console\Application |
$enableCoreCommands | boolean | Whether to enable the commands provided by the core framework. | yii\console\Application |
$extensions | array | List of installed Yii extensions. | yii\base\Application |
$id | string | An ID that uniquely identifies this module among other modules which have the same parent. | yii\base\Module |
$language | string | The language that is meant to be used for end users. | yii\base\Application |
$layout | string|boolean | The layout that should be applied for views in this application. | yii\base\Application |
$loadedModules | array | List of loaded modules indexed by their class names. | yii\base\Application |
$module | yii\base\Module | The parent module of this module. | yii\base\Module |
$name | string | The application name. | yii\base\Application |
$params | array | Custom module parameters (name => value). | yii\base\Module |
$requestedAction | yii\base\Action | The requested Action. | yii\base\Application |
$requestedParams | array | The parameters supplied to the requested action. | yii\base\Application |
$requestedRoute | string | The requested route | yii\base\Application |
$sourceLanguage | string | The language that the application is written in. | yii\base\Application |
$state | integer | The current application state during a request handling life cycle. | yii\base\Application |
Public Methods
Method | Description | Defined By |
---|---|---|
__construct() | Constructor. | yii\console\Application |
afterAction() | This method is invoked right after an action within this module is executed. | yii\base\Module |
beforeAction() | This method is invoked right before an action within this module is executed. | yii\base\Module |
coreCommands() | Returns the configuration of the built-in commands. | yii\console\Application |
coreComponents() | Returns the configuration of core application components. | yii\console\Application |
createController() | Creates a controller instance based on the given route. | yii\base\Module |
createControllerByID() | Creates a controller based on the given controller ID. | yii\base\Module |
end() | Terminates the application. | yii\base\Application |
get() | yii\base\Module | |
getAssetManager() | Returns the asset manager. | yii\base\Application |
getAuthManager() | Returns the auth manager for this application. | yii\base\Application |
getBasePath() | Returns the root directory of the module. | yii\base\Module |
getCache() | Returns the cache component. | yii\base\Application |
getControllerPath() | Returns the directory that contains the controller classes according to $controllerNamespace. | yii\base\Module |
getDb() | Returns the database connection component. | yii\base\Application |
getErrorHandler() | Returns the error handler component. | yii\console\Application |
getFormatter() | Returns the formatter component. | yii\base\Application |
getI18n() | Returns the internationalization (i18n) component. | yii\base\Application |
getInstance() | Returns the currently requested instance of this module class. | yii\base\Module |
getLayoutPath() | Returns the directory that contains layout view files for this module. | yii\base\Module |
getLog() | Returns the log dispatcher component. | yii\base\Application |
getMailer() | Returns the mailer component. | yii\base\Application |
getModule() | Retrieves the child module of the specified ID. | yii\base\Module |
getModules() | Returns the sub-modules in this module. | yii\base\Module |
getRequest() | Returns the request component. | yii\console\Application |
getResponse() | Returns the response component. | yii\console\Application |
getRuntimePath() | Returns the directory that stores runtime files. | yii\base\Application |
getSecurity() | Returns the security component. | yii\base\Application |
getTimeZone() | Returns the time zone used by this application. | yii\base\Application |
getUniqueId() | Returns an ID that uniquely identifies this module among all modules within the current application. | yii\base\Application |
getUrlManager() | Returns the URL manager for this application. | yii\base\Application |
getVendorPath() | Returns the directory that stores vendor files. | yii\base\Application |
getVersion() | Returns current module version. | yii\base\Module |
getView() | Returns the view object. | yii\base\Application |
getViewPath() | Returns the directory that contains the view files for this module. | yii\base\Module |
handleRequest() | Handles the specified request. | yii\console\Application |
has() | yii\base\Module | |
hasModule() | Checks whether the child module of the specified ID exists. | yii\base\Module |
init() | Initialize the application. | yii\console\Application |
preInit() | Pre-initializes the application. | yii\base\Application |
run() | Runs the application. | yii\base\Application |
runAction() | Runs a controller action specified by a route. | yii\console\Application |
setAliases() | Defines path aliases. | yii\base\Module |
setBasePath() | Sets the root directory of the application and the @app alias. | yii\base\Application |
setContainer() | Configures \Yii::$container with the $config. | yii\base\Application |
setInstance() | Sets the currently requested instance of this module class. | yii\base\Module |
setLayoutPath() | Sets the directory that contains the layout files. | yii\base\Module |
setModule() | Adds a sub-module to this module. | yii\base\Module |
setModules() | Registers sub-modules in the current module. | yii\base\Module |
setRuntimePath() | Sets the directory that stores runtime files. | yii\base\Application |
setTimeZone() | Sets the time zone used by this application. | yii\base\Application |
setVendorPath() | Sets the directory that stores vendor files. | yii\base\Application |
setVersion() | Sets current module version. | yii\base\Module |
setViewPath() | Sets the directory that contains the view files. | yii\base\Module |
Protected Methods
Method | Description | Defined By |
---|---|---|
bootstrap() | Initializes extensions and executes bootstrap components. | yii\base\Application |
defaultVersion() | Returns default module version. | yii\base\Module |
loadConfig() | Loads the configuration. | yii\console\Application |
registerErrorHandler() | Registers the errorHandler component as a PHP error handler. | yii\base\Application |
Events
Event | Type | Description | Defined By |
---|---|---|---|
EVENT_AFTER_ACTION | yii\base\ActionEvent | An event raised after executing a controller action. | yii\base\Module |
EVENT_AFTER_REQUEST | yii\base\Event | An event raised after the application successfully handles a request (before the response is sent out). | yii\base\Application |
EVENT_BEFORE_ACTION | yii\base\ActionEvent | An event raised before executing a controller action. | yii\base\Module |
EVENT_BEFORE_REQUEST | yii\base\Event | An event raised before the application starts to handle a request. | yii\base\Application |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
OPTION_APPCONFIG | 'appconfig' | The option name for specifying the application configuration file path. | yii\console\Application |
STATE_AFTER_REQUEST | 4 | Application state used by $state: application is triggering EVENT_AFTER_REQUEST. . | yii\base\Application |
STATE_BEFORE_REQUEST | 2 | Application state used by $state: application is triggering EVENT_BEFORE_REQUEST. | yii\base\Application |
STATE_BEGIN | 0 | Application state used by $state: application just started. | yii\base\Application |
STATE_END | 6 | Application state used by $state: application has ended. | yii\base\Application |
STATE_HANDLING_REQUEST | 3 | Application state used by $state: application is handling the request. | yii\base\Application |
STATE_INIT | 1 | Application state used by $state: application is initializing. | yii\base\Application |
STATE_SENDING_RESPONSE | 5 | Application state used by $state: application is about to send response. | yii\base\Application |
Property Details
The currently active controller instance
The default route of this application. Defaults to 'help',
meaning the help
command.
Whether to enable the commands provided by the core framework. Defaults to true.
Method Details
Constructor.
public void __construct ( $config = [] ) | ||
$config | array | Name-value pairs that will be used to initialize the object properties. Note that the configuration must contain both $id and \yii\console\basePath. |
throws | yii\base\InvalidConfigException | if either $id or \yii\console\basePath configuration is missing. |
---|
Returns the configuration of the built-in commands.
public array coreCommands ( ) | ||
return | array | The configuration of the built-in commands. |
---|
Returns the configuration of core application components.
public void coreComponents ( ) |
Returns the error handler component.
public yii\console\ErrorHandler getErrorHandler ( ) | ||
return | yii\console\ErrorHandler | The error handler application component. |
---|
Returns the request component.
public yii\console\Request getRequest ( ) | ||
return | yii\console\Request | The request component. |
---|
Returns the response component.
public yii\console\Response getResponse ( ) | ||
return | yii\console\Response | The response component. |
---|
Handles the specified request.
public yii\console\Response handleRequest ( $request ) | ||
$request | yii\console\Request | The request to be handled |
return | yii\console\Response | The resulting response |
---|
Initialize the application.
public void init ( ) |
Loads the configuration.
This method will check if the command line option OPTION_APPCONFIG is specified. If so, the corresponding file will be loaded as the application configuration. Otherwise, the configuration provided as the parameter will be returned back.
protected array loadConfig ( $config ) | ||
$config | array | The configuration provided in the constructor. |
return | array | The actual configuration to be used by the application. |
---|
Runs a controller action specified by a route.
This method parses the specified route and creates the corresponding child module(s), controller and action instances. It then calls yii\console\Controller::runAction() to run the action with the given parameters. If the route is empty, the method will use $defaultRoute.
For example, to run public function actionTest($a, $b)
assuming that the controller has options the following
code should be used:
\Yii::$app->runAction('controller/test', ['option' => 'value', $a, $b]);
public integer|yii\console\Response runAction ( $route, $params = [] ) | ||
$route | string | The route that specifies the action. |
$params | array | The parameters to be passed to the action |
return | integer|yii\console\Response | The result of the action. This can be either an exit code or Response object.
Exit code 0 means normal, and other values mean abnormal. Exit code of |
---|---|---|
throws | yii\console\Exception | if the route is invalid |