Abstract Class yii\base\Application
Inheritance | yii\base\Application » yii\base\Module » yii\di\ServiceLocator » yii\base\Component » yii\base\BaseObject |
---|---|
Implements | yii\base\Configurable |
Subclasses | yii\console\Application, yii\web\Application |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/base/Application.php |
Application is the base class for all application classes.
For more details and usage information on Application, see the guide article on applications.
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$aliases | array | List of path aliases to be defined. The array keys are alias names (must
start with @ ) and the array values are the corresponding paths or aliases. See setAliases() for an
example. This property is write-only. |
yii\base\Module |
$assetManager | yii\web\AssetManager | The asset manager application component. This property is read-only. | yii\base\Application |
$authManager | yii\rbac\ManagerInterface | The auth manager application component. Null is returned if auth manager is not configured. This property is read-only. | yii\base\Application |
$basePath | string | The root directory of the application. | yii\base\Application |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | 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. Null if the component is not enabled. This property is read-only. | yii\base\Application |
$charset | string | The charset currently used for the application. | yii\base\Application |
$components | array | The list of the component definitions or the loaded component instances (ID => definition or instance). | yii\di\ServiceLocator |
$container | array | Values given in terms of name-value pairs. This property is write-only. | yii\base\Application |
$controller | yii\base\Controller | The currently active controller instance | yii\base\Application |
$controllerMap | array | Mapping from controller ID to controller configurations. | yii\base\Module |
$controllerNamespace | string | The namespace that controller classes are located in. | yii\base\Application |
$controllerPath | string | The directory that contains the controller classes. This property is read-only. | yii\base\Module |
$db | yii\db\Connection | The database connection. This property is read-only. | yii\base\Application |
$defaultRoute | string | The default route of this module. | yii\base\Module |
$errorHandler | yii\web\ErrorHandler|yii\console\ErrorHandler | The error handler application component. This property is read-only. | yii\base\Application |
$extensions | array | List of installed Yii extensions. | yii\base\Application |
$formatter | yii\i18n\Formatter | The formatter application component. This property is read-only. | yii\base\Application |
$i18n | yii\i18n\I18N | The internationalization application component. This property is read-only. | 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 in this application. | yii\base\Application |
$layoutPath | string | The root directory of layout files. Defaults to "$viewPath/layouts". | 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. This property is read-only. | yii\base\Application |
$mailer | yii\mail\MailerInterface | The mailer application component. This property is read-only. | yii\base\Application |
$module | yii\base\Module|null | 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\web\Request|yii\console\Request | The request component. This property is read-only. | yii\base\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\web\Response|yii\console\Response | The response component. This property is read-only. | yii\base\Application |
$runtimePath | string | The directory that stores runtime files. Defaults to the "runtime" subdirectory under $basePath. | yii\base\Application |
$security | yii\base\Security | The security application component. This property is read-only. | 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. This property is read-only. | yii\base\Application |
$urlManager | yii\web\UrlManager | The URL manager for this application. This property is read-only. | yii\base\Application |
$vendorPath | string | The directory that stores vendor files. Defaults to "vendor" directory under $basePath. | yii\base\Application |
$version | string | The version of this module. Note that the type of this property differs in getter and setter. See getVersion() and setVersion() for details. | yii\base\Module |
$view | yii\base\View|yii\web\View | The view application component that is used to render various view files. This property is read-only. | yii\base\Application |
$viewPath | string | The root directory of view files. Defaults to "$basePath/views". | yii\base\Module |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\BaseObject |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\Application |
__get() | Returns the value of an object property. | yii\base\BaseObject |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\BaseObject |
__set() | Sets value of an object property. | yii\base\BaseObject |
__unset() | Sets an object property to null. | yii\base\BaseObject |
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\BaseObject |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\BaseObject |
className() | Returns the fully qualified name of this class. | yii\base\BaseObject |
clear() | Removes the component from the locator. | yii\di\ServiceLocator |
coreComponents() | Returns the configuration of core application components. | yii\base\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. | yii\base\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() | Returns the request component. | yii\base\Application |
getResponse() | Returns the response component. | yii\base\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. | yii\base\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\BaseObject |
hasModule() | Checks whether the child module of the specified ID exists. | yii\base\Module |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\BaseObject |
init() | Initializes the module. | yii\base\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. | yii\base\Module |
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 |
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 |
Protected Methods
Method | Description | Defined By |
---|---|---|
bootstrap() | Initializes extensions and executes bootstrap components. | yii\base\Application |
defaultVersion() | Returns default module version. | yii\base\Module |
registerErrorHandler() | Registers the errorHandler component as a PHP error handler. | yii\base\Application |
Events
Event | Type | Description | Defined By |
---|---|---|---|
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 |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
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 |
Property Details
The asset manager application component. This property is read-only.
The auth manager application component. Null is returned if auth manager is not configured. This property is read-only.
The root directory of the application.
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 yii\base\BootstrapInterface, its bootstrap() method will be also be called.
The cache application component. Null if the component is not enabled. This property is read-only.
The charset currently used for the application.
Values given in terms of name-value pairs. This property is write-only.
The currently active controller instance
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.
The database connection. This property is read-only.
The error handler application component. This property is read-only.
List 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 yii\base\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
.
The formatter application component. This property is read-only.
The internationalization application component. This property is read-only.
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).
See also $sourceLanguage.
The layout that should be applied for views in this application. Defaults to 'main'. If this is false, layout will be disabled.
List of loaded modules indexed by their class names.
The log dispatcher application component. This property is read-only.
The mailer application component. This property is read-only.
The application name.
The request component. This property is read-only.
The requested Action. If null, it means the request cannot be resolved into an action.
The parameters supplied to the requested action.
The requested route
The response component. This property is read-only.
The directory that stores runtime files. Defaults to the "runtime" subdirectory under $basePath.
The security application component. This property is read-only.
The language that the application is written in. This mainly refers to the language that the messages and view files are written in.
See also $language.
The current application state during a request handling life cycle. This property is managed by the application. Do not modify this property.
The time zone used by this application.
The unique ID of the module. This property is read-only.
The URL manager for this application. This property is read-only.
The directory that stores vendor files. Defaults to "vendor" directory under $basePath.
The view application component that is used to render various view files. This property is read-only.
Method Details
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 |
---|
Initializes 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.
protected void bootstrap ( ) |
Returns the configuration of core application components.
See also set().
public void coreComponents ( ) |
Terminates the application.
This method replaces the exit()
function by ensuring the application life cycle is completed
before terminating the application.
public void end ( $status = 0, $response = null ) | ||
$status | integer | The exit status (value 0 means normal exit while other values mean abnormal exit). |
$response | yii\base\Response | The response to be sent. If not set, the default application $response component will be used. |
throws | yii\base\ExitException | if the application is in testing mode |
---|
Returns the asset manager.
public yii\web\AssetManager getAssetManager ( ) | ||
return | yii\web\AssetManager | The asset manager application component. |
---|
Returns the auth manager for this application.
public yii\rbac\ManagerInterface getAuthManager ( ) | ||
return | yii\rbac\ManagerInterface | The auth manager application component. Null is returned if auth manager is not configured. |
---|
Returns the cache component.
public yii\caching\CacheInterface getCache ( ) | ||
return | yii\caching\CacheInterface | The cache application component. Null if the component is not enabled. |
---|
Returns the database connection component.
public yii\db\Connection getDb ( ) | ||
return | yii\db\Connection | The database connection. |
---|
Returns the error handler component.
public yii\web\ErrorHandler|yii\console\ErrorHandler getErrorHandler ( ) | ||
return | yii\web\ErrorHandler|yii\console\ErrorHandler | The error handler application component. |
---|
Returns the formatter component.
public yii\i18n\Formatter getFormatter ( ) | ||
return | yii\i18n\Formatter | The formatter application component. |
---|
Returns the internationalization (i18n) component.
public yii\i18n\I18N getI18n ( ) | ||
return | yii\i18n\I18N | The internationalization application component. |
---|
Returns the log dispatcher component.
public yii\log\Dispatcher getLog ( ) | ||
return | yii\log\Dispatcher | The log dispatcher application component. |
---|
Returns the mailer component.
public yii\mail\MailerInterface getMailer ( ) | ||
return | yii\mail\MailerInterface | The mailer application component. |
---|
Returns the request component.
public yii\web\Request|yii\console\Request getRequest ( ) | ||
return | yii\web\Request|yii\console\Request | The request component. |
---|
Returns the response component.
public yii\web\Response|yii\console\Response getResponse ( ) | ||
return | yii\web\Response|yii\console\Response | The response component. |
---|
Returns the directory that stores runtime files.
public string getRuntimePath ( ) | ||
return | string | The directory that stores runtime files. Defaults to the "runtime" subdirectory under $basePath. |
---|
Returns the security component.
public yii\base\Security getSecurity ( ) | ||
return | yii\base\Security | The security application component. |
---|
Returns 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.
See also https://secure.php.net/manual/en/function.date-default-timezone-get.php.
public string getTimeZone ( ) | ||
return | string | The time zone used by this application. |
---|
Returns 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.
public string getUniqueId ( ) | ||
return | string | The unique ID of the module. |
---|
Returns the URL manager for this application.
public yii\web\UrlManager getUrlManager ( ) | ||
return | yii\web\UrlManager | The URL manager for this application. |
---|
Returns the directory that stores vendor files.
public string getVendorPath ( ) | ||
return | string | The directory that stores vendor files. Defaults to "vendor" directory under $basePath. |
---|
Returns the view object.
public yii\base\View|yii\web\View getView ( ) | ||
return | yii\base\View|yii\web\View | The view application component that is used to render various view files. |
---|
Handles the specified request.
This method should return an instance of yii\base\Response or its child class which represents the handling result of the request.
public abstract yii\base\Response handleRequest ( $request ) | ||
$request | yii\base\Request | The request to be handled |
return | yii\base\Response | The resulting response |
---|
Initializes 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.
public void init ( ) |
Pre-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.
public void preInit ( &$config ) | ||
$config | array | The application configuration |
throws | yii\base\InvalidConfigException |
---|
Registers the errorHandler component as a PHP error handler.
protected void registerErrorHandler ( &$config ) | ||
$config | array | Application config |
Runs the application.
This is the main entrance of an application.
public integer run ( ) | ||
return | integer | The exit status (0 means normal, non-zero values mean abnormal) |
---|
Sets the root directory of the application and the @app alias.
This method can only be invoked at the beginning of the constructor.
public void setBasePath ( $path ) | ||
$path | string | The root directory of the application. |
throws | yii\base\InvalidArgumentException | if the directory does not exist. |
---|
Configures Yii::$container with the $config.
public void setContainer ( $config ) | ||
$config | array | Values given in terms of name-value pairs |
Sets the directory that stores runtime files.
public void setRuntimePath ( $path ) | ||
$path | string | The directory that stores runtime files. |
Sets 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.
See also https://secure.php.net/manual/en/function.date-default-timezone-set.php.
public void setTimeZone ( $value ) | ||
$value | string | The time zone used by this application. |
Sets the directory that stores vendor files.
public void setVendorPath ( $path ) | ||
$path | string | The directory that stores vendor files. |
Event Details
An event raised after the application successfully handles a request (before the response is sent out).
An event raised before the application starts to handle a request.