Class yii\console\Application
Inheritance | yii\console\Application » yii\base\Application » yii\base\Module » yii\di\ServiceLocator » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
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 |
---|---|---|---|
$aliases | array | List of path aliases to be defined. | yii\base\Module |
$assetManager | yii\web\AssetManager | The asset manager application component. | yii\base\Application |
$authManager | yii\rbac\ManagerInterface | The auth manager application component. | yii\base\Application |
$basePath | string | The root directory of the module. | yii\base\Module |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
$bootstrap | array | List of components that should be run during the application bootstrapping process. | yii\base\Application |
$cache | yii\caching\CacheInterface | The cache application component. | yii\base\Application |
$charset | string | The charset currently used for the application. | yii\base\Application |
$components | array | The list of the component definitions or the loaded component instances (ID => definition or instance). | yii\di\ServiceLocator |
$container | array | Values given in terms of name-value pairs | 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 |
$controllerPath | string | The directory that contains the controller classes. | yii\base\Module |
$db | yii\db\Connection | The database connection. | 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 |
$errorHandler | yii\console\ErrorHandler | The error handler application component. | yii\console\Application |
$extensions | array | List of installed Yii extensions. | yii\base\Application |
$formatter | yii\i18n\Formatter | The formatter application component. | yii\base\Application |
$i18n | yii\i18n\I18N | The internationalization application component. | 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 |
$layoutPath | string | The root directory of layout files. | yii\base\Module |
$loadedModules | array | List of loaded modules indexed by their class names. | yii\base\Application |
$log | yii\log\Dispatcher | The log dispatcher application component. | yii\base\Application |
$mailer | yii\mail\MailerInterface | The mailer application component. | yii\base\Application |
$module | yii\base\Module | The parent module of this module. | yii\base\Module |
$modules | array | The modules (indexed by their IDs). | yii\base\Module |
$name | string | The application name. | yii\base\Application |
$params | array | Custom module parameters (name => value). | yii\base\Module |
$request | yii\console\Request | The request component. | yii\console\Application |
$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 |
$response | yii\console\Response | The response component. | yii\console\Application |
$runtimePath | string | The directory that stores runtime files. | yii\base\Application |
$security | yii\base\Security | The security application component. | 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 |
$timeZone | string | The time zone used by this application. | yii\base\Application |
$uniqueId | string | The unique ID of the module. | yii\base\Application |
$urlManager | yii\web\UrlManager | The URL manager for this application. | yii\base\Application |
$vendorPath | string | The directory that stores vendor files. | yii\base\Application |
$version | string | The version of this module. | yii\base\Module |
$view | yii\base\View|yii\web\View | The view application component that is used to render various view files. | yii\base\Application |
$viewPath | string | The root directory of view files. | yii\base\Module |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Component |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\console\Application |
__get() | Returns the value of a component property. | yii\base\Component |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component |
__set() | Sets the value of a component property. | yii\base\Component |
__unset() | Sets a component property to be null. | yii\base\Component |
afterAction() | This method is invoked right after an action within this module is executed. | yii\base\Module |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
beforeAction() | This method is invoked right before an action within this module is executed. | yii\base\Module |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
clear() | Removes the component from the locator. | yii\di\ServiceLocator |
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 |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
end() | Terminates the application. | yii\base\Application |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
get() | Returns the component instance with the specified ID. | 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 |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
getCache() | Returns the cache component. | yii\base\Application |
getComponents() | Returns the list of the component definitions or the loaded component instances. | yii\di\ServiceLocator |
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() | Returns a value indicating whether the locator has the specified component definition or has instantiated the component. | yii\base\Module |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component |
hasModule() | Checks whether the child module of the specified ID exists. | yii\base\Module |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component |
init() | Initialize the application. | yii\console\Application |
off() | Detaches an existing event handler from this component. | yii\base\Component |
on() | Attaches an event handler to an event. | yii\base\Component |
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 |
set() | Registers a component definition with this locator. | yii\di\ServiceLocator |
setAliases() | Defines path aliases. | yii\base\Module |
setBasePath() | Sets the root directory of the application and the @app alias. | yii\base\Application |
setComponents() | Registers a set of component definitions in this locator. | yii\di\ServiceLocator |
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 |
trigger() | Triggers an event. | yii\base\Component |
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.
The error handler application component.
The request component.
The response component.
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 $basePath. |
throws | yii\base\InvalidConfigException |
---|
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 |