Application
API reference › yii › console › Application
Application represents a console application.
Application extends from 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
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 helpProperties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$actionMap | array<string, | Mapping from action IDs to standalone Action class configurations. Default: []. Available since 22.0. | yii\ |
$actionNamespace | string| | The namespace where standalone Action subclasses are located for convention-based discovery, parallel to $controllerNamespace for controllers. Default: null. Available since 22.0. | yii\ |
$aliases write-only | array | List of path aliases to be defined. | yii\ |
$assetManager read-only | yii\ | The asset manager application component. | yii\ |
$authManager read-only | yii\| | The auth manager application component or null if it’s not configured. | yii\ |
$basePath | string | The root directory of the application. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$bootstrap | array | List of components that should be run during the application bootstrapping process. Default: []. | yii\ |
$cache read-only | yii\| | The cache application component. | yii\ |
$charset | string | The charset currently used for the application. Default: 'UTF-8'. | yii\ |
$components | array | The list of the component definitions or the loaded component instances (ID => definition or instance). | yii\ |
$container write-only | array | Values given in terms of name-value pairs. | yii\ |
$controller | Controller| | The currently active controller instance Default: null. | Application |
$controllerMap | array | Mapping from controller ID to controller configurations. Default: []. | yii\ |
$controllerNamespace | string | The namespace that controller classes are located in. Default: 'app\. | yii\ |
$controllerPath | string | The directory that contains the controller classes. | yii\ |
$db read-only | yii\ | The database connection. | yii\ |
$defaultRoute | string | The default route of this application. Default: 'help'. | Application |
$enableCoreCommands | bool | Whether to enable the commands provided by the core framework. Default: true. | Application |
$errorHandler read-only | ErrorHandler | The error handler application component. | Application |
$extensions | array| | List of installed Yii extensions. Default: null. | yii\ |
$formatter read-only | yii\ | The formatter application component. | yii\ |
$i18n read-only | yii\ | The internationalization application component. | yii\ |
$id | string | An ID that uniquely identifies this module among other modules which have the same parent. Default: null. | yii\ |
$language | string | The language that is meant to be used for end users. Default: 'en-US'. | yii\ |
$layout | string| | The layout that should be applied for views in this application. Default: 'main'. | yii\ |
$layoutPath | string | The root directory of layout files. | yii\ |
$loadedModules | array | List of loaded modules indexed by their class names. Default: []. | yii\ |
$log read-only | yii\ | The log dispatcher application component. | yii\ |
$mailer read-only | yii\ | The mailer application component. | yii\ |
$module | yii\| | The parent module of this module. null if this module does not have a parent. Default: null. | yii\ |
$modules | array | The modules (indexed by their IDs). | yii\ |
$name | string | The application name. Default: 'My Application'. | yii\ |
$params | array | Custom module parameters (name => value). Default: []. | yii\ |
$request read-only | Request | The request component. | Application |
$requestedAction | yii\<yii\>| | The requested Action. Default: null. | yii\ |
$requestedParams | array| | The parameters supplied to the requested action. Default: null. | yii\ |
$requestedRoute | string | The requested route Default: null. | yii\ |
$response read-only | Response | The response component. | Application |
$runtimePath | string | The directory that stores runtime files. | yii\ |
$security read-only | yii\ | The security application component. | yii\ |
$sourceLanguage | string | The language that the application is written in. Default: 'en-US'. | yii\ |
$state | int | The current application state during a request handling life cycle. Default: null. | yii\ |
$timeZone | string | The time zone used by this application. | yii\ |
$uniqueId read-only | string | The unique ID of the module. | yii\ |
$urlManager read-only | yii\ | The URL manager for this application. | yii\ |
$vendorPath | string | The directory that stores vendor files. | yii\ |
$version | string| | The version of this module. | yii\ |
$view read-only | yii\|yii\ | The view application component that is used to render various view files. | yii\ |
$viewPath | string | The root directory of view files. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | Application |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Getter magic method. | yii\ |
__isset() | Checks if a property value is null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
afterAction() | This method is invoked right after an action within this module is executed. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeAction() | This method is invoked right before an action within this module is executed. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
clear() | Removes the component from the locator. | yii\ |
coreCommands() | Returns the configuration of the built-in commands. | Application |
coreComponents() | Returns the configuration of core application components. | Application |
createController() | Creates a controller instance based on the given route. | yii\ |
createControllerByID() | Creates a controller based on the given controller ID. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
end() | Terminates the application. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
get() | Returns the component instance with the specified ID. | yii\ |
getAssetManager() | Returns the asset manager. | yii\ |
getAuthManager() | Returns the auth manager for this application. | yii\ |
getBasePath() | Returns the root directory of the module. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getCache() | Returns the cache component. | yii\ |
getComponents() | Returns the list of the component definitions or the loaded component instances. | yii\ |
getControllerPath() | Returns the directory that contains the controller classes according to $controllerNamespace. | yii\ |
getDb() | Returns the database connection component. | yii\ |
getErrorHandler() | Returns the error handler component. | Application |
getFormatter() | Returns the formatter component. | yii\ |
getI18n() | Returns the internationalization (i18n) component. | yii\ |
getInstance() static | Returns the currently requested instance of this module class. | yii\ |
getLayoutPath() | Returns the directory that contains layout view files for this module. | yii\ |
getLog() | Returns the log dispatcher component. | yii\ |
getMailer() | Returns the mailer component. | yii\ |
getModule() | Retrieves the child module of the specified ID. | yii\ |
getModules() | Returns the sub-modules in this module. | yii\ |
getRequest() | Returns the request component. | Application |
getResponse() | Returns the response component. | Application |
getRuntimePath() | Returns the directory that stores runtime files. | yii\ |
getSecurity() | Returns the security component. | yii\ |
getTimeZone() | Returns the time zone used by this application. | yii\ |
getUniqueId() | Returns an ID that uniquely identifies this module among all modules within the current application. | yii\ |
getUrlManager() | Returns the URL manager for this application. | yii\ |
getVendorPath() | Returns the directory that stores vendor files. | yii\ |
getVersion() | Returns current module version. | yii\ |
getView() | Returns the view object. | yii\ |
getViewPath() | Returns the directory that contains the view files for this module. | yii\ |
handleRequest() | Handles the specified request. | Application |
has() | Returns a value indicating whether the locator has the specified component definition or has instantiated the component. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasModule() | Checks whether the child module of the specified ID exists. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initialize the application. | Application |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
preInit() | Pre-initializes the application. | yii\ |
run() | Runs the application. | yii\ |
runAction() | Runs a controller action specified by a route. | Application |
set() | Registers a component definition with this locator. | yii\ |
setAliases() | Defines path aliases. | yii\ |
setBasePath() | Sets the root directory of the application and the @app alias. | yii\ |
setComponents() | Registers a set of component definitions in this locator. | yii\ |
setContainer() | Configures Yii::$container with the $config. | yii\ |
setControllerPath() | Sets the directory that contains the controller classes. | yii\ |
setInstance() static | Sets the currently requested instance of this module class. | yii\ |
setLayoutPath() | Sets the directory that contains the layout files. | yii\ |
setModule() | Adds a sub-module to this module. | yii\ |
setModules() | Registers sub-modules in the current module. | yii\ |
setRuntimePath() | Sets the directory that stores runtime files. | yii\ |
setTimeZone() | Sets the time zone used by this application. | yii\ |
setVendorPath() | Sets the directory that stores vendor files. | yii\ |
setVersion() | Sets current module version. | yii\ |
setViewPath() | Sets the directory that contains the view files. | yii\ |
trigger() | Triggers an event. | yii\ |
bootstrap() protected | Initializes extensions and executes bootstrap components. | yii\ |
createStandaloneAction() protected | Resolves a route to a standalone Action class through convention-based discovery under $actionNamespace. | yii\ |
defaultVersion() protected | Returns default module version. | yii\ |
loadConfig() protected | Loads the configuration. | Application |
registerErrorHandler() protected | Registers the errorHandler component as a PHP error handler. | yii\ |
runMappedAction() protected | Runs a standalone action registered in $actionMap. | yii\ |
runStandaloneAction() protected | Runs a standalone Action instance through the standard module/action event lifecycle. | yii\ |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_BEFORE_REQUEST | yii\ | An event raised before the application starts to handle a request. Event name: 'beforeRequest'. | yii\ |
EVENT_AFTER_REQUEST | yii\ | An event raised after the application successfully handles a request (before the response is sent out). Event name: 'afterRequest'. | yii\ |
EVENT_BEFORE_ACTION | yii\ | An event raised before executing a controller action. Event name: 'beforeAction'. | yii\ |
EVENT_AFTER_ACTION | yii\ | An event raised after executing a controller action. Event name: 'afterAction'. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
OPTION_APPCONFIG | 'appconfig' | The option name for specifying the application configuration file path. | Application |
STATE_BEGIN | 0 | Application state used by $state: application just started. | yii\ |
STATE_INIT | 1 | Application state used by $state: application is initializing. | yii\ |
STATE_BEFORE_REQUEST | 2 | Application state used by $state: application is triggering EVENT_BEFORE_REQUEST. | yii\ |
STATE_HANDLING_REQUEST | 3 | Application state used by $state: application is handling the request. | yii\ |
STATE_AFTER_REQUEST | 4 | Application state used by $state: application is triggering EVENT_AFTER_REQUEST. | yii\ |
STATE_SENDING_RESPONSE | 5 | Application state used by $state: application is about to send response. | yii\ |
STATE_END | 6 | Application state used by $state: application has ended. | yii\ |
Property details
Section titled “Property details”$defaultRoute
Section titled “$defaultRoute ”public string $defaultRoute = 'help'The default route of this application.
Defaults to ‘help’,
meaning the help command.
$enableCoreCommands
Section titled “$enableCoreCommands ”public bool $enableCoreCommands = trueWhether to enable the commands provided by the core framework.
Defaults to true.
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(mixed $config = [])Constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | mixed | 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: if either$idor$basePathconfiguration is missing.
coreCommands()
Section titled “coreCommands() ”public function coreCommands(): arrayReturns the configuration of the built-in commands.
Returns
array: The configuration of the built-in commands.
coreComponents()
Section titled “coreComponents() ”public function coreComponents(): arrayReturns the configuration of core application components.
Returns
array
getErrorHandler()
Section titled “getErrorHandler() ”public function getErrorHandler(): yii\console\ErrorHandlerReturns the error handler component.
Returns
ErrorHandler: The error handler application component.
getRequest()
Section titled “getRequest() ”public function getRequest(): yii\console\RequestReturns the request component.
Returns
Request: The request component.
getResponse()
Section titled “getResponse() ”public function getResponse(): yii\console\ResponseReturns the response component.
Returns
Response: The response component.
handleRequest()
Section titled “handleRequest() ”public function handleRequest(yii\console\Request $request): yii\console\ResponseHandles the specified request.
init()
Section titled “init() ”public function init(): mixedInitialize the application.
Returns
mixed
runAction()
Section titled “runAction() ”public function runAction(string $route, array $params = []): int|yii\console\Response|nullRuns 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 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]);Parameters
| Parameter | Type | Description |
|---|---|---|
$route | string | The route that specifies the action. |
$params | array | The parameters to be passed to the action |
Returns
int|Response|null: 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 treated as 0 as well.
Throws
Exception: if the route is invalid
loadConfig()
Section titled “loadConfig() ”protected function loadConfig(array $config): arrayLoads 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.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array | The configuration provided in the constructor. |
Returns
array: The actual configuration to be used by the application.