Skip to content
PHP Ecosystem
Explore

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 help
PropertyTypeDescriptionDefined by
$actionMaparray<string, class-string|array<string, mixed>>Mapping from action IDs to standalone Action class configurations. Default: []. Available since 22.0.yii\base\Module
$actionNamespacestring|nullThe namespace where standalone Action subclasses are located for convention-based discovery, parallel to $controllerNamespace for controllers. Default: null. Available since 22.0.yii\base\Module
$aliases write-onlyarrayList of path aliases to be defined.yii\base\Module
$assetManager read-onlyyii\web\AssetManagerThe asset manager application component.yii\base\Application
$authManager read-onlyyii\rbac\ManagerInterface|nullThe auth manager application component or null if it’s not configured.yii\base\Application
$basePathstringThe root directory of the application.yii\base\Application
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$bootstraparrayList of components that should be run during the application bootstrapping process. Default: [].yii\base\Application
$cache read-onlyyii\caching\CacheInterface|nullThe cache application component.yii\base\Application
$charsetstringThe charset currently used for the application. Default: 'UTF-8'.yii\base\Application
$componentsarrayThe list of the component definitions or the loaded component instances (ID => definition or instance).yii\di\ServiceLocator
$container write-onlyarrayValues given in terms of name-value pairs.yii\base\Application
$controllerController|nullThe currently active controller instance Default: null.Application
$controllerMaparrayMapping from controller ID to controller configurations. Default: [].yii\base\Module
$controllerNamespacestringThe namespace that controller classes are located in. Default: 'app\controllers'.yii\base\Application
$controllerPathstringThe directory that contains the controller classes.yii\base\Module
$db read-onlyyii\db\ConnectionThe database connection.yii\base\Application
$defaultRoutestringThe default route of this application. Default: 'help'.Application
$enableCoreCommandsboolWhether to enable the commands provided by the core framework. Default: true.Application
$errorHandler read-onlyErrorHandlerThe error handler application component.Application
$extensionsarray|nullList of installed Yii extensions. Default: null.yii\base\Application
$formatter read-onlyyii\i18n\FormatterThe formatter application component.yii\base\Application
$i18n read-onlyyii\i18n\I18NThe internationalization application component.yii\base\Application
$idstringAn ID that uniquely identifies this module among other modules which have the same parent. Default: null.yii\base\Module
$languagestringThe language that is meant to be used for end users. Default: 'en-US'.yii\base\Application
$layoutstring|boolThe layout that should be applied for views in this application. Default: 'main'.yii\base\Application
$layoutPathstringThe root directory of layout files.yii\base\Module
$loadedModulesarrayList of loaded modules indexed by their class names. Default: [].yii\base\Application
$log read-onlyyii\log\DispatcherThe log dispatcher application component.yii\base\Application
$mailer read-onlyyii\mail\MailerInterfaceThe mailer application component.yii\base\Application
$moduleyii\base\Module|nullThe parent module of this module. null if this module does not have a parent. Default: null.yii\base\Module
$modulesarrayThe modules (indexed by their IDs).yii\base\Module
$namestringThe application name. Default: 'My Application'.yii\base\Application
$paramsarrayCustom module parameters (name => value). Default: [].yii\base\Module
$request read-onlyRequestThe request component.Application
$requestedActionyii\base\Action<yii\base\Controller>|nullThe requested Action. Default: null.yii\base\Application
$requestedParamsarray|nullThe parameters supplied to the requested action. Default: null.yii\base\Application
$requestedRoutestringThe requested route Default: null.yii\base\Application
$response read-onlyResponseThe response component.Application
$runtimePathstringThe directory that stores runtime files.yii\base\Application
$security read-onlyyii\base\SecurityThe security application component.yii\base\Application
$sourceLanguagestringThe language that the application is written in. Default: 'en-US'.yii\base\Application
$stateintThe current application state during a request handling life cycle. Default: null.yii\base\Application
$timeZonestringThe time zone used by this application.yii\base\Application
$uniqueId read-onlystringThe unique ID of the module.yii\base\Application
$urlManager read-onlyyii\web\UrlManagerThe URL manager for this application.yii\base\Application
$vendorPathstringThe directory that stores vendor files.yii\base\Application
$versionstring|callable|nullThe version of this module.yii\base\Module
$view read-onlyyii\base\View|yii\web\ViewThe view application component that is used to render various view files.yii\base\Application
$viewPathstringThe root directory of view files.yii\base\Module
MethodDescriptionDefined by
__construct()Constructor.Application
__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
__get()Getter magic method.yii\di\ServiceLocator
__isset()Checks if a property value is 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() staticReturns 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.Application
coreComponents()Returns the configuration of core application components.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.Application
getFormatter()Returns the formatter component.yii\base\Application
getI18n()Returns the internationalization (i18n) component.yii\base\Application
getInstance() staticReturns 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.Application
getResponse()Returns the response component.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.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.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.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
setControllerPath()Sets the directory that contains the controller classes.yii\base\Module
setInstance() staticSets 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() protectedInitializes extensions and executes bootstrap components.yii\base\Application
createStandaloneAction() protectedResolves a route to a standalone Action class through convention-based discovery under $actionNamespace.yii\base\Module
defaultVersion() protectedReturns default module version.yii\base\Module
loadConfig() protectedLoads the configuration.Application
registerErrorHandler() protectedRegisters the errorHandler component as a PHP error handler.yii\base\Application
runMappedAction() protectedRuns a standalone action registered in $actionMap.yii\base\Module
runStandaloneAction() protectedRuns a standalone Action instance through the standard module/action event lifecycle.yii\base\Module
EventTypeDescriptionDefined by
EVENT_BEFORE_REQUESTyii\base\EventAn event raised before the application starts to handle a request. Event name: 'beforeRequest'.yii\base\Application
EVENT_AFTER_REQUESTyii\base\EventAn event raised after the application successfully handles a request (before the response is sent out). Event name: 'afterRequest'.yii\base\Application
EVENT_BEFORE_ACTIONyii\base\ActionEventAn event raised before executing a controller action. Event name: 'beforeAction'.yii\base\Module
EVENT_AFTER_ACTIONyii\base\ActionEventAn event raised after executing a controller action. Event name: 'afterAction'.yii\base\Module
ConstantValueDescriptionDefined by
OPTION_APPCONFIG'appconfig'The option name for specifying the application configuration file path.Application
STATE_BEGIN0Application state used by $state: application just started.yii\base\Application
STATE_INIT1Application state used by $state: application is initializing.yii\base\Application
STATE_BEFORE_REQUEST2Application state used by $state: application is triggering EVENT_BEFORE_REQUEST.yii\base\Application
STATE_HANDLING_REQUEST3Application state used by $state: application is handling the request.yii\base\Application
STATE_AFTER_REQUEST4Application state used by $state: application is triggering EVENT_AFTER_REQUEST.yii\base\Application
STATE_SENDING_RESPONSE5Application state used by $state: application is about to send response.yii\base\Application
STATE_END6Application state used by $state: application has ended.yii\base\Application
public string $defaultRoute = 'help'

The default route of this application.

Defaults to ‘help’, meaning the help command.

public bool $enableCoreCommands = true

Whether to enable the commands provided by the core framework.

Defaults to true.

public function __construct(mixed $config = [])

Constructor.

Parameters

ParameterTypeDescription
$configmixedName-value pairs that will be used to initialize the object properties. Note that the configuration must contain both $id and $basePath.

Throws

public function coreCommands(): array

Returns the configuration of the built-in commands.

Returns

array: The configuration of the built-in commands.

public function coreComponents(): array

Returns the configuration of core application components.

Returns

array

public function getErrorHandler(): yii\console\ErrorHandler

Returns the error handler component.

Returns

ErrorHandler: The error handler application component.

public function getRequest(): yii\console\Request

Returns the request component.

Returns

Request: The request component.

public function getResponse(): yii\console\Response

Returns the response component.

Returns

Response: The response component.

public function handleRequest(yii\console\Request $request): yii\console\Response

Handles the specified request.

Parameters

ParameterTypeDescription
$requestRequestThe request to be handled

Returns

Response: The resulting response

public function init(): mixed

Initialize the application.

Returns

mixed

public function runAction(string $route, array $params = []): int|yii\console\Response|null

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 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

ParameterTypeDescription
$routestringThe route that specifies the action.
$paramsarrayThe 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

protected function loadConfig(array $config): array

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.

Parameters

ParameterTypeDescription
$configarrayThe configuration provided in the constructor.

Returns

array: The actual configuration to be used by the application.