Class yii\console\Application
Application 代表一个控制台应用程序。
Application 继承自 yii\base\Application 通过提供特定于控制台请求的功能。 特别是, 它处理控制台请求通过基于命令的方法:
- 控制台应用程序包含一个或多个可能的用户命令;
- 每个用户命令都实现为继承 yii\console\Controller 的类;
- 用户指定在命令行上运行哪个命令;
- 该命令使用指定的参数处理用户请求。
命令类应位于 $controllerNamespace 指定的命名空间下。
它们的命名应遵循与控制器相同的命名约定。例如,help
命令
使用 HelpController
类实现。
要运行控制台应用程序,请在命令行中输入以下内容:
yii <route> [--param1=value1 --param2 ...]
其中 <route>
指的是 ModuleID/ControllerID/ActionID
形式的控制器路由
(例如 sitemap/create
),和 param1
,param2
指的是一组命名参数
将用于初始化控制器动作(例如 --since=0
指定一个 since
参数
其值为 0 并且将相应的 $since
参数传递给动作方法)。
默认提供 help
命令,列出可用命令并显示其用法。
要使用此命令,只需键入:
yii help
公共属性
属性 | 类型 | 描述 | 被定义在 |
---|---|---|---|
$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 组件定义列表或加载的组件实例(ID => definition or instance)。 | yii\di\ServiceLocator |
$container | array | Values given in terms of name-value pairs | yii\base\Application |
$controller | yii\console\Controller | 当前运行的控制器实例 | yii\console\Application |
$controllerMap | array | Mapping from controller ID to controller configurations. | yii\base\Module |
$controllerNamespace | string | The namespace that controller classes are in. | yii\base\Module |
$controllerPath | string | The directory that contains the controller classes. | yii\base\Module |
$db | yii\db\Connection | The database connection. | yii\base\Application |
$defaultRoute | string | 此应用程序的默认路由。默认为 'help',
表示 help 命令。 |
yii\console\Application |
$enableCoreCommands | boolean | 是否启用核心框架提供的命令。 默认为 true。 | yii\console\Application |
$errorHandler | yii\console\ErrorHandler | 错误处理程序应用程序组件。 | 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 within this module. | yii\base\Module |
$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 | 请求组件。 | 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 | 响应组件。 | 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\Module |
$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 |
公共方法
方法 | 描述 | 被定义在 |
---|---|---|
__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() | Getter 魔术方法。 重写此方法以支持访问诸如读取属性之类的组件。 | yii\di\ServiceLocator |
__isset() | 检查属性值是否为 null。 此方法通过检查是否已加载命名组件来覆盖父类的实现。 | yii\di\ServiceLocator |
__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() | 从定位器移除组件。 | yii\di\ServiceLocator |
coreCommands() | 返回内置命令的配置。 | 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() | 返回具有指定 ID 的组件实例。 | yii\di\ServiceLocator |
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() | 返回组件定义列表或已加载的组件实例。 | 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() | 返回错误处理程序组件。 | 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() | 返回请求组件。 | yii\console\Application |
getResponse() | 返回响应组件。 | 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\Module |
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() | 处理指定的请求。 | yii\console\Application |
has() | 返回一个值,该值表示定位器是否具有指定的组件定义或是否已实例化该组件。
此方法根据 $checkInstance 的值返回不同的结果。 |
yii\di\ServiceLocator |
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() | 初始化应用程序。 | 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() | 运行路由指定的控制器操作。 此方法解析指定的路由并创建相应的子模块,控制器和动作实例。 然后调用 yii\console\Controller::runAction() 通过给定的参数来运行动作。 如果路由为空,则方法将使用 $defaultRoute。 | yii\console\Application |
set() | 用定位器注册一个组件。 | yii\di\ServiceLocator |
setAliases() | Defines path aliases. | yii\base\Module |
setBasePath() | Sets the root directory of the module. | yii\base\Module |
setComponents() | 在定位器中注册一组组件定义。 | 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 |
受保护的方法
方法 | 描述 | 被定义在 |
---|---|---|
bootstrap() | Initializes extensions and executes bootstrap components. | yii\base\Application |
defaultVersion() | Returns default module version. | yii\base\Module |
loadConfig() | 加载配置。 此方法将检查是否指定了命令行选项 OPTION_APPCONFIG。 如果是,则将相应的文件作为应用程序配置加载。 否则,将返回作为参数提供的配置。 | yii\console\Application |
registerErrorHandler() | Registers the errorHandler component as a PHP error handler. | yii\base\Application |
Events
事件 | 类型 | 描述 | 被定义在 |
---|---|---|---|
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 |
常量
常量 | 值 | 描述 | 被定义在 |
---|---|---|---|
OPTION_APPCONFIG | 'appconfig' | 用于指定应用程序配置文件路径的选项名称。 | 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 |
属性详情
当前运行的控制器实例
此应用程序的默认路由。默认为 'help',
表示 help
命令。
是否启用核心框架提供的命令。 默认为 true。
错误处理程序应用程序组件。
请求组件。
响应组件。
方法详情
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 |
---|
返回内置命令的配置。
public array coreCommands() | ||
return | array | 内置命令的配置。 |
---|
Returns the configuration of core application components.
public void coreComponents() |
返回错误处理程序组件。
public yii\console\ErrorHandler getErrorHandler() | ||
return | yii\console\ErrorHandler | 错误处理程序应用程序组件。 |
---|
返回请求组件。
public yii\console\Request getRequest() | ||
return | yii\console\Request | 请求组件。 |
---|
返回响应组件。
public yii\console\Response getResponse() | ||
return | yii\console\Response | 响应组件。 |
---|
处理指定的请求。
public yii\console\Response handleRequest($request) | ||
$request | yii\console\Request | 要处理的请求 |
return | yii\console\Response | 生成的响应 |
---|
初始化应用程序。
public void init() |
加载配置。 此方法将检查是否指定了命令行选项 OPTION_APPCONFIG。 如果是,则将相应的文件作为应用程序配置加载。 否则,将返回作为参数提供的配置。
protected array loadConfig($config) | ||
$config | array | 构造函数中提供的配置。 |
return | array | 应用程序使用的实际配置。 |
---|
运行路由指定的控制器操作。 此方法解析指定的路由并创建相应的子模块,控制器和动作实例。 然后调用 yii\console\Controller::runAction() 通过给定的参数来运行动作。 如果路由为空,则方法将使用 $defaultRoute。
例如,运行 public function actionTest($a, $b)
假设控制器有选项
应使用以下代码:
\Yii::$app->runAction('controller/test', ['option' => 'value', $a, $b]);
public integer|yii\console\Response runAction($route, $params = []) | ||
$route | string | 指定动作的路由。 |
$params | array | 要传递给动作的参数 |
return | integer|yii\console\Response | 动作的结果。这可以是退出码或 Response 对象。
退出代码 0 表示正常,其他值表示异常。退出代码 |
---|---|---|
throws | yii\console\Exception | 如果路由无效 |