PHP Class yii\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
[--param1=value1 --param2 ...]
~~~
where 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
~~~
Datei anzeigen
Open project: yiisoft/yii2
Class Usage Examples
Public Properties
Property |
Type |
Description |
|
$controller |
|
the currently active controller instance |
|
$defaultRoute |
|
the default route of this application. Defaults to 'help',
meaning the help command. |
|
$enableCoreCommands |
|
whether to enable the commands provided by the core framework.
Defaults to true. |
|
Public Methods
Protected Methods
Method Details
__construct()
public method
coreCommands()
public method
Returns the configuration of the built-in commands.
coreComponents()
public method
getErrorHandler()
public method
Returns the error handler component.
public getErrorHandler ( ) : yii\console\ErrorHandler |
return |
yii\console\ErrorHandler |
the error handler application component. |
getRequest()
public method
Returns the request component.
public getRequest ( ) : yii\console\Request |
return |
yii\console\Request |
the request component. |
getResponse()
public method
Returns the response component.
public getResponse ( ) : Response |
return |
Response |
the response component. |
handleRequest()
public method
Handles the specified request.
public handleRequest ( yii\console\Request $request ) : Response |
$request |
yii\console\Request |
the request to be handled |
return |
Response |
the resulting response |
Initialize the application.
loadConfig()
protected method
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 loadConfig ( array $config ) : array |
$config |
array |
the configuration provided in the constructor. |
return |
array |
the actual configuration to be used by the application. |
runAction()
public method
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:
php
\Yii::$app->runAction('controller/test', ['option' => 'value', $a, $b]);
public runAction ( string $route, array $params = [] ) : integer | Response |
$route |
string |
the route that specifies the action. |
$params |
array |
the parameters to be passed to the action |
return |
integer | 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. |
Property Details
$controller public_oe property
the currently active controller instance
$defaultRoute public_oe property
the default route of this application. Defaults to 'help',
meaning the help command.
$enableCoreCommands public_oe property
whether to enable the commands provided by the core framework.
Defaults to true.
public $enableCoreCommands |