Class yii\console\Application

Inheritanceyii\console\Application » yii\base\Application » yii\base\Module » yii\di\ServiceLocator » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Available since version2.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

Hide inherited properties

PropertyTypeDescriptionDefined 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

Hide inherited methods

MethodDescriptionDefined 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() {@inheritdoc} 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() {@inheritdoc} 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() {@inheritdoc} 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() {@inheritdoc} 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

Hide inherited methods

MethodDescriptionDefined 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

Hide inherited events

EventTypeDescriptionDefined 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

Hide inherited constants

ConstantValueDescriptionDefined 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

$controller public property

The currently active controller instance

$defaultRoute public property

The default route of this application. Defaults to 'help', meaning the help command.

public string $defaultRoute 'help'
$enableCoreCommands public property

Whether to enable the commands provided by the core framework. Defaults to true.

Method Details

__construct() public method

{@inheritdoc}

public void __construct ( $config = [] )
$config
coreCommands() public method

Returns the configuration of the built-in commands.

public array coreCommands ( )
return array

The configuration of the built-in commands.

coreComponents() public method

{@inheritdoc}

public void coreComponents ( )
getErrorHandler() public method

Returns the error handler component.

public yii\console\ErrorHandler getErrorHandler ( )
return yii\console\ErrorHandler

The error handler application component.

getRequest() public method

Returns the request component.

public yii\console\Request getRequest ( )
return yii\console\Request

The request component.

getResponse() public method

Returns the response component.

public yii\console\Response getResponse ( )
return yii\console\Response

The response component.

handleRequest() public method

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

init() public method

Initialize the application.

public void init ( )
loadConfig() protected method

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.

runAction() public method

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 null is treaded as 0 as well.

throws yii\console\Exception

if the route is invalid