Application
API reference › yii › base › Application
2 subclasses
Application is the base class for all application classes.
For more details and usage information on Application, see the guide article on applications.
Properties
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. | Module |
$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. | Module |
$aliases write-only | array | List of path aliases to be defined. | Module |
$assetManager read-only | yii\ | The asset manager application component. | Application |
$authManager read-only | yii\| | The auth manager application component or null if it’s not configured. | Application |
$basePath | string | The root directory of the application. | Application |
$behaviors read-only | array<Behavior<static>> | List of behaviors attached to this component. | Component |
$bootstrap | array | List of components that should be run during the application bootstrapping process. Default: []. | Application |
$cache read-only | yii\| | The cache application component. | Application |
$charset | string | The charset currently used for the application. Default: 'UTF-8'. | Application |
$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. | Application |
$controller | Controller| | The currently active controller instance Default: null. | Application |
$controllerMap | array | Mapping from controller ID to controller configurations. Default: []. | Module |
$controllerNamespace | string | The namespace that controller classes are located in. Default: 'app\. | Application |
$controllerPath | string | The directory that contains the controller classes. | Module |
$db read-only | yii\ | The database connection. | Application |
$defaultRoute | string | The default route of this module. Default: 'default'. | Module |
$errorHandler read-only | yii\|yii\ | The error handler application component. | Application |
$extensions | array| | List of installed Yii extensions. Default: null. | Application |
$formatter read-only | yii\ | The formatter application component. | Application |
$i18n read-only | yii\ | The internationalization application component. | Application |
$id | string | An ID that uniquely identifies this module among other modules which have the same parent. Default: null. | Module |
$language | string | The language that is meant to be used for end users. Default: 'en-US'. | Application |
$layout | string| | The layout that should be applied for views in this application. Default: 'main'. | Application |
$layoutPath | string | The root directory of layout files. | Module |
$loadedModules | array | List of loaded modules indexed by their class names. Default: []. | Application |
$log read-only | yii\ | The log dispatcher application component. | Application |
$mailer read-only | yii\ | The mailer application component. | Application |
$module | Module| | The parent module of this module. null if this module does not have a parent. Default: null. | Module |
$modules | array | The modules (indexed by their IDs). | Module |
$name | string | The application name. Default: 'My Application'. | Application |
$params | array | Custom module parameters (name => value). Default: []. | Module |
$request read-only | yii\|yii\ | The request component. | Application |
$requestedAction | Action<Controller>| | The requested Action. Default: null. | Application |
$requestedParams | array| | The parameters supplied to the requested action. Default: null. | Application |
$requestedRoute | string | The requested route Default: null. | Application |
$response read-only | yii\|yii\ | The response component. | Application |
$runtimePath | string | The directory that stores runtime files. | Application |
$security read-only | Security | The security application component. | Application |
$sourceLanguage | string | The language that the application is written in. Default: 'en-US'. | Application |
$state | int | The current application state during a request handling life cycle. Default: null. | Application |
$timeZone | string | The time zone used by this application. | Application |
$uniqueId read-only | string | The unique ID of the module. | Application |
$urlManager read-only | yii\ | The URL manager for this application. | Application |
$vendorPath | string | The directory that stores vendor files. | Application |
$version | string| | The version of this module. | Module |
$view read-only | View|yii\ | The view application component that is used to render various view files. | Application |
$viewPath | string | The root directory of view files. | Module |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | Application |
__call() | Calls the named method which is not a class method. | Component |
__clone() | This method is called after the object is created by cloning an existing one. | Component |
__get() | Getter magic method. | yii\ |
__isset() | Checks if a property value is null. | yii\ |
__set() | Sets the value of a component property. | Component |
__unset() | Sets a component property to be null. | Component |
afterAction() | This method is invoked right after an action within this module is executed. | Module |
attachBehavior() | Attaches a behavior to this component. | Component |
attachBehaviors() | Attaches a list of behaviors to the component. | Component |
beforeAction() | This method is invoked right before an action within this module is executed. | Module |
behaviors() | Returns a list of behaviors that this component should behave as. | Component |
canGetProperty() | Returns a value indicating whether a property can be read. | Component |
canSetProperty() | Returns a value indicating whether a property can be set. | Component |
className() static | Returns the fully qualified name of this class. | BaseObject |
clear() | Removes the component from the locator. | yii\ |
coreComponents() | Returns the configuration of core application components. | Application |
createController() | Creates a controller instance based on the given route. | Module |
createControllerByID() | Creates a controller based on the given controller ID. | Module |
detachBehavior() | Detaches a behavior from the component. | Component |
detachBehaviors() | Detaches all behaviors from the component. | Component |
end() | Terminates the application. | Application |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | Component |
get() | Returns the component instance with the specified ID. | Module |
getAssetManager() | Returns the asset manager. | Application |
getAuthManager() | Returns the auth manager for this application. | Application |
getBasePath() | Returns the root directory of the module. | Module |
getBehavior() | Returns the named behavior object. | Component |
getBehaviors() | Returns all behaviors attached to this component. | Component |
getCache() | Returns the cache component. | Application |
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. | Module |
getDb() | Returns the database connection component. | Application |
getErrorHandler() | Returns the error handler component. | Application |
getFormatter() | Returns the formatter component. | Application |
getI18n() | Returns the internationalization (i18n) component. | Application |
getInstance() static | Returns the currently requested instance of this module class. | Module |
getLayoutPath() | Returns the directory that contains layout view files for this module. | Module |
getLog() | Returns the log dispatcher component. | Application |
getMailer() | Returns the mailer component. | Application |
getModule() | Retrieves the child module of the specified ID. | Module |
getModules() | Returns the sub-modules in this module. | Module |
getRequest() | Returns the request component. | Application |
getResponse() | Returns the response component. | Application |
getRuntimePath() | Returns the directory that stores runtime files. | Application |
getSecurity() | Returns the security component. | Application |
getTimeZone() | Returns the time zone used by this application. | Application |
getUniqueId() | Returns an ID that uniquely identifies this module among all modules within the current application. | Application |
getUrlManager() | Returns the URL manager for this application. | Application |
getVendorPath() | Returns the directory that stores vendor files. | Application |
getVersion() | Returns current module version. | Module |
getView() | Returns the view object. | Application |
getViewPath() | Returns the directory that contains the view files for this module. | Module |
handleRequest() abstract | Handles the specified request. | Application |
has() | Returns a value indicating whether the locator has the specified component definition or has instantiated the component. | Module |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | Component |
hasMethod() | Returns a value indicating whether a method is defined. | Component |
hasModule() | Checks whether the child module of the specified ID exists. | Module |
hasProperty() | Returns a value indicating whether a property is defined for this component. | Component |
init() | Initializes the module. | Application |
off() | Detaches an existing event handler from this component. | Component |
on() | Attaches an event handler to an event. | Component |
preInit() | Pre-initializes the application. | Application |
run() | Runs the application. | Application |
runAction() | Runs a controller action specified by a route. | Module |
set() | Registers a component definition with this locator. | yii\ |
setAliases() | Defines path aliases. | Module |
setBasePath() | Sets the root directory of the application and the @app alias. | Application |
setComponents() | Registers a set of component definitions in this locator. | yii\ |
setContainer() | Configures Yii::$container with the $config. | Application |
setControllerPath() | Sets the directory that contains the controller classes. | Module |
setInstance() static | Sets the currently requested instance of this module class. | Module |
setLayoutPath() | Sets the directory that contains the layout files. | Module |
setModule() | Adds a sub-module to this module. | Module |
setModules() | Registers sub-modules in the current module. | Module |
setRuntimePath() | Sets the directory that stores runtime files. | Application |
setTimeZone() | Sets the time zone used by this application. | Application |
setVendorPath() | Sets the directory that stores vendor files. | Application |
setVersion() | Sets current module version. | Module |
setViewPath() | Sets the directory that contains the view files. | Module |
trigger() | Triggers an event. | Component |
bootstrap() protected | Initializes extensions and executes bootstrap components. | Application |
createStandaloneAction() protected | Resolves a route to a standalone Action class through convention-based discovery under $actionNamespace. | Module |
defaultVersion() protected | Returns default module version. | Module |
registerErrorHandler() protected | Registers the errorHandler component as a PHP error handler. | Application |
runMappedAction() protected | Runs a standalone action registered in $actionMap. | Module |
runStandaloneAction() protected | Runs a standalone Action instance through the standard module/action event lifecycle. | Module |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_BEFORE_REQUEST | Event | An event raised before the application starts to handle a request. Event name: 'beforeRequest'. | Application |
EVENT_AFTER_REQUEST | Event | An event raised after the application successfully handles a request (before the response is sent out). Event name: 'afterRequest'. | Application |
EVENT_BEFORE_ACTION | ActionEvent | An event raised before executing a controller action. Event name: 'beforeAction'. | Module |
EVENT_AFTER_ACTION | ActionEvent | An event raised after executing a controller action. Event name: 'afterAction'. | Module |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
STATE_BEGIN | 0 | Application state used by $state: application just started. | Application |
STATE_INIT | 1 | Application state used by $state: application is initializing. | Application |
STATE_BEFORE_REQUEST | 2 | Application state used by $state: application is triggering EVENT_BEFORE_REQUEST. | Application |
STATE_HANDLING_REQUEST | 3 | Application state used by $state: application is handling the request. | Application |
STATE_AFTER_REQUEST | 4 | Application state used by $state: application is triggering EVENT_AFTER_REQUEST. | Application |
STATE_SENDING_RESPONSE | 5 | Application state used by $state: application is about to send response. | Application |
STATE_END | 6 | Application state used by $state: application has ended. | Application |
Property details
Section titled “Property details”$bootstrap
Section titled “$bootstrap ”public array $bootstrap = []List of components that should be run during the application bootstrapping process.
Each component may be specified in one of the following formats:
- an application component ID as specified via
$components. - a module ID as specified via
$modules. - a class name.
- a configuration array.
- a Closure
During the bootstrapping process, each component will be instantiated. If the component class
implements BootstrapInterface, its bootstrap() method
will be also be called.
$cache
Section titled “$cache ”public yii\caching\CacheInterface|null $cacheThe cache application component.
Null if the component is not enabled.
Access
Read-only (virtual property).
$controllerNamespace
Section titled “$controllerNamespace ”public string $controllerNamespace = 'app\controllers'The namespace that controller classes are located in.
This namespace will be used to load controller classes by prepending it to the controller class name.
The default namespace is app\controllers.
Please refer to the guide about class autoloading for more details.
$extensions
Section titled “$extensions ”public array|null $extensions = nullList of installed Yii extensions.
Each array element represents a single extension with the following structure:
[ 'name' => 'extension name', 'version' => 'version number', 'bootstrap' => 'BootstrapClassName', // optional, may also be a configuration array 'alias' => [ '@alias1' => 'to/path1', '@alias2' => 'to/path2', ],]The “bootstrap” class listed above will be instantiated during the application
bootstrapping process. If the class implements BootstrapInterface,
its bootstrap() method will be also be called.
If not set explicitly in the application config, this property will be populated with the contents of
@vendor/yiisoft/extensions.php.
$language
Section titled “$language ”public string $language = 'en-US'The language that is meant to be used for end users.
It is recommended that you
use IETF language tags. For example, en stands
for English, while en-US stands for English (United States).
$layout
Section titled “$layout ”public string|bool $layout = 'main'The layout that should be applied for views in this application.
Defaults to ‘main’. If this is false, layout will be disabled.
$requestedAction
Section titled “$requestedAction ”public yii\base\Action<yii\base\Controller>|null $requestedAction = nullThe requested Action.
If null, it means the request cannot be resolved into an action.
$runtimePath
Section titled “$runtimePath ”public string $runtimePathThe directory that stores runtime files.
Defaults to the “runtime”
subdirectory under $basePath.
$sourceLanguage
Section titled “$sourceLanguage ”public string $sourceLanguage = 'en-US'The language that the application is written in.
This mainly refers to the language that the messages and view files are written in.
$state
Section titled “$state ”public int $state = nullThe current application state during a request handling life cycle.
This property is managed by the application. Do not modify this property.
$vendorPath
Section titled “$vendorPath ”public string $vendorPathThe directory that stores vendor files.
Defaults to “vendor” directory under
$basePath.
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(array<array-key, mixed> $config = [])Constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array<array-key, | Name-value pairs that will be used to initialize the object properties. Note that the configuration must contain both $id and $basePath. |
Throws
InvalidConfigException: if either$idor$basePathconfiguration is missing.
coreComponents()
Section titled “coreComponents() ”public function coreComponents(): arrayReturns the configuration of core application components.
end()
Section titled “end() ”public function end(int $status = 0, yii\base\Response|null $response = null): mixedTerminates the application.
This method replaces the exit() function by ensuring the application life cycle is completed
before terminating the application.
Parameters
| Parameter | Type | Description |
|---|---|---|
$status | int | The exit status (value 0 means normal exit while other values mean abnormal exit). |
$response | Response| | The response to be sent. If not set, the default application $response component will be used. |
Returns
mixed
Throws
ExitException: if the application is in testing mode
getAssetManager()
Section titled “getAssetManager() ”public function getAssetManager(): yii\web\AssetManagerReturns the asset manager.
Returns
yii\web\AssetManager: The asset manager application component.
getAuthManager()
Section titled “getAuthManager() ”public function getAuthManager(): yii\rbac\ManagerInterface|nullReturns the auth manager for this application.
Returns
yii\rbac\ManagerInterface|null: The auth manager application component or null if it’s not configured.
getCache()
Section titled “getCache() ”public function getCache(): yii\caching\CacheInterface|nullReturns the cache component.
Returns
yii\caching\CacheInterface|null: The cache application component. Null if the component is not enabled.
getDb()
Section titled “getDb() ”public function getDb(): yii\db\ConnectionReturns the database connection component.
Returns
yii\db\Connection: The database connection.
getErrorHandler()
Section titled “getErrorHandler() ”public function getErrorHandler(): yii\web\ErrorHandler|yii\console\ErrorHandlerReturns the error handler component.
Returns
yii\web\ErrorHandler|yii\console\ErrorHandler: The error handler application component.
getFormatter()
Section titled “getFormatter() ”public function getFormatter(): yii\i18n\FormatterReturns the formatter component.
Returns
yii\i18n\Formatter: The formatter application component.
getI18n()
Section titled “getI18n() ”public function getI18n(): yii\i18n\I18NReturns the internationalization (i18n) component.
Returns
yii\i18n\I18N: The internationalization application component.
getLog()
Section titled “getLog() ”public function getLog(): yii\log\DispatcherReturns the log dispatcher component.
Returns
yii\log\Dispatcher: The log dispatcher application component.
getMailer()
Section titled “getMailer() ”public function getMailer(): yii\mail\MailerInterfaceReturns the mailer component.
Returns
yii\mail\MailerInterface: The mailer application component.
Throws
InvalidConfigException: If this component is not configured.
getRequest()
Section titled “getRequest() ”public function getRequest(): yii\web\Request|yii\console\RequestReturns the request component.
Returns
yii\web\Request|yii\console\Request: The request component.
getResponse()
Section titled “getResponse() ”public function getResponse(): yii\web\Response|yii\console\ResponseReturns the response component.
Returns
yii\web\Response|yii\console\Response: The response component.
getRuntimePath()
Section titled “getRuntimePath() ”public function getRuntimePath(): stringReturns the directory that stores runtime files.
Returns
string: The directory that stores runtime files. Defaults to the “runtime” subdirectory under $basePath.
getSecurity()
Section titled “getSecurity() ”public function getSecurity(): yii\base\SecurityReturns the security component.
Returns
Security: The security application component.
getTimeZone()
Section titled “getTimeZone() ”public function getTimeZone(): stringReturns the time zone used by this application.
This is a simple wrapper of PHP function date_default_timezone_get(). If time zone is not configured in php.ini or application config, it will be set to UTC by default.
Returns
string: The time zone used by this application.
See also
getUniqueId()
Section titled “getUniqueId() ”public function getUniqueId(): stringReturns an ID that uniquely identifies this module among all modules within the current application.
Since this is an application instance, it will always return an empty string.
Returns
string: The unique ID of the module.
getUrlManager()
Section titled “getUrlManager() ”public function getUrlManager(): yii\web\UrlManagerReturns the URL manager for this application.
Returns
yii\web\UrlManager: The URL manager for this application.
getVendorPath()
Section titled “getVendorPath() ”public function getVendorPath(): stringReturns the directory that stores vendor files.
Returns
string: The directory that stores vendor files. Defaults to “vendor” directory under $basePath.
getView()
Section titled “getView() ”public function getView(): yii\base\View|yii\web\ViewReturns the view object.
Returns
View|yii\web\View: The view application component that is used to render various view files.
handleRequest()
Section titled “handleRequest() ”abstract public function handleRequest(yii\base\Request $request): yii\base\ResponseHandles the specified request.
This method should return an instance of Response or its child class
which represents the handling result of the request.
init()
Section titled “init() ”public function init(): mixedInitializes the module.
This method is called after the module is created and initialized with property values given in configuration.
The default implementation will initialize $controllerNamespace if it is not set.
If you override this method, please make sure you call the parent implementation.
Returns
mixed
preInit()
Section titled “preInit() ”public function preInit(array &$config): mixedPre-initializes the application.
This method is called at the beginning of the application constructor. It initializes several important application properties. If you override this method, please make sure you call the parent implementation.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array | The application configuration |
Returns
mixed
Throws
InvalidConfigException: if either$idor$basePathconfiguration is missing.
run()
Section titled “run() ”public function run(): intRuns the application.
This is the main entrance of an application.
Returns
int: The exit status (0 means normal, non-zero values mean abnormal)
setBasePath()
Section titled “setBasePath() ”public function setBasePath(string $path): mixedSets the root directory of the application and the @app alias.
This method can only be invoked at the beginning of the constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | The root directory of the application. |
Returns
mixed
Throws
InvalidArgumentException: if the directory does not exist.
setContainer()
Section titled “setContainer() ”public function setContainer(array $config): mixedConfigures Yii::$container with the $config.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array | Values given in terms of name-value pairs |
Returns
mixed
Since
2.0.11
setRuntimePath()
Section titled “setRuntimePath() ”public function setRuntimePath(string $path): mixedSets the directory that stores runtime files.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | The directory that stores runtime files. |
Returns
mixed
setTimeZone()
Section titled “setTimeZone() ”public function setTimeZone(string $value): mixedSets the time zone used by this application.
This is a simple wrapper of PHP function date_default_timezone_set(). Refer to the php manual for available timezones.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | string | The time zone used by this application. |
Returns
mixed
See also
setVendorPath()
Section titled “setVendorPath() ”public function setVendorPath(string $path): mixedSets the directory that stores vendor files.
Parameters
| Parameter | Type | Description |
|---|---|---|
$path | string | The directory that stores vendor files. |
Returns
mixed
bootstrap()
Section titled “bootstrap() ”protected function bootstrap(): mixedInitializes extensions and executes bootstrap components.
This method is called by init() after the application has been fully configured.
If you override this method, make sure you also call the parent implementation.
Returns
mixed
registerErrorHandler()
Section titled “registerErrorHandler() ”protected function registerErrorHandler(array &$config): mixedRegisters the errorHandler component as a PHP error handler.
Parameters
| Parameter | Type | Description |
|---|---|---|
$config | array | Application config |
Returns
mixed