Application
extends Module
in package
Application is the base class for all application classes.
For more details and usage information on Application, see the guide article on applications.
Tags
Table of Contents
Constants
- EVENT_AFTER_ACTION = 'afterAction'
- EVENT_AFTER_REQUEST = 'afterRequest'
- EVENT_BEFORE_ACTION = 'beforeAction'
- EVENT_BEFORE_REQUEST = 'beforeRequest'
- STATE_AFTER_REQUEST = 4
- Application state used by [[state]]: application is triggering [[EVENT_AFTER_REQUEST]].
- STATE_BEFORE_REQUEST = 2
- Application state used by [[state]]: application is triggering [[EVENT_BEFORE_REQUEST]].
- STATE_BEGIN = 0
- Application state used by [[state]]: application just started.
- STATE_END = 6
- Application state used by [[state]]: application has ended.
- STATE_HANDLING_REQUEST = 3
- Application state used by [[state]]: application is handling the request.
- STATE_INIT = 1
- Application state used by [[state]]: application is initializing.
- STATE_SENDING_RESPONSE = 5
- Application state used by [[state]]: application is about to send response.
Properties
- $aliases : array<string|int, mixed>
- $assetManager : AssetManager
- $authManager : ManagerInterface|null
- $basePath : string
- $basePath : string
- $behaviors : array<string|int, Behavior>
- $bootstrap : array<string|int, mixed>
- $cache : CacheInterface|null
- $charset : string
- $components : array<string|int, mixed>
- $container : array<string|int, mixed>
- $controller : Controller
- $controllerMap : array<string|int, mixed>
- $controllerNamespace : string
- $controllerPath : string
- $db : Connection
- $defaultRoute : string
- $errorHandler : ErrorHandler|ErrorHandler
- $extensions : array<string|int, mixed>|null
- $formatter : Formatter
- $i18n : I18N
- $id : string
- $language : string
- $layout : string|bool
- $layoutPath : string
- $loadedModules : array<string|int, mixed>
- $log : Dispatcher
- $mailer : MailerInterface
- $module : Module|null
- $modules : array<string|int, mixed>
- $name : string
- $params : array<string|int, mixed>
- $request : Request|Request
- $requestedAction : Action|null
- $requestedParams : array<string|int, mixed>
- $requestedRoute : string
- $response : Response|Response
- $runtimePath : string
- $security : Security
- $sourceLanguage : string
- $state : int
- $timeZone : string
- $uniqueId : string
- $uniqueId : string
- $urlManager : UrlManager
- $vendorPath : string
- $version : string
- $view : View|View
- $viewPath : string
- $_basePath : string
- $_behaviors : array<string|int, Behavior>|null
- $_components : array<string|int, mixed>
- $_controllerPath : string
- $_definitions : array<string|int, mixed>
- $_events : array<string|int, mixed>
- $_eventWildcards : array<string|int, mixed>
- $_layoutPath : string
- $_modules : array<string|int, mixed>
- $_runtimePath : mixed
- $_vendorPath : mixed
- $_version : string|callable|null
- $_viewPath : string
Methods
- __call() : mixed
- Calls the named method which is not a class method.
- __clone() : mixed
- This method is called after the object is created by cloning an existing one.
- __construct() : mixed
- Constructor.
- __get() : mixed
- Getter magic method.
- __isset() : bool
- Checks if a property value is null.
- __set() : mixed
- Sets the value of a component property.
- __unset() : mixed
- Sets a component property to be null.
- afterAction() : mixed
- This method is invoked right after an action within this module is executed.
- attachBehavior() : Behavior
- Attaches a behavior to this component.
- attachBehaviors() : mixed
- Attaches a list of behaviors to the component.
- beforeAction() : bool
- This method is invoked right before an action within this module is executed.
- behaviors() : array<string|int, mixed>
- Returns a list of behaviors that this component should behave as.
- canGetProperty() : bool
- Returns a value indicating whether a property can be read.
- canSetProperty() : bool
- Returns a value indicating whether a property can be set.
- className() : string
- Returns the fully qualified name of this class.
- clear() : mixed
- Removes the component from the locator.
- coreComponents() : array<string|int, mixed>
- Returns the configuration of core application components.
- createController() : array<string|int, mixed>|bool
- Creates a controller instance based on the given route.
- createControllerByID() : Controller|null
- Creates a controller based on the given controller ID.
- detachBehavior() : Behavior|null
- Detaches a behavior from the component.
- detachBehaviors() : mixed
- Detaches all behaviors from the component.
- end() : mixed
- Terminates the application.
- ensureBehaviors() : mixed
- Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
- get() : object|null
- Returns the component instance with the specified ID.
- getAssetManager() : AssetManager
- Returns the asset manager.
- getAuthManager() : ManagerInterface|null
- Returns the auth manager for this application.
- getBasePath() : string
- Returns the root directory of the module.
- getBehavior() : Behavior|null
- Returns the named behavior object.
- getBehaviors() : array<string|int, Behavior>
- Returns all behaviors attached to this component.
- getCache() : CacheInterface|null
- Returns the cache component.
- getComponents() : array<string|int, mixed>
- Returns the list of the component definitions or the loaded component instances.
- getControllerPath() : string
- Returns the directory that contains the controller classes according to [[controllerNamespace]].
- getDb() : Connection
- Returns the database connection component.
- getErrorHandler() : ErrorHandler|ErrorHandler
- Returns the error handler component.
- getFormatter() : Formatter
- Returns the formatter component.
- getI18n() : I18N
- Returns the internationalization (i18n) component.
- getInstance() : static|null
- Returns the currently requested instance of this module class.
- getLayoutPath() : string
- Returns the directory that contains layout view files for this module.
- getLog() : Dispatcher
- Returns the log dispatcher component.
- getMailer() : MailerInterface
- Returns the mailer component.
- getModule() : Module|null
- Retrieves the child module of the specified ID.
- getModules() : array<string|int, mixed>
- Returns the sub-modules in this module.
- getRequest() : Request|Request
- Returns the request component.
- getResponse() : Response|Response
- Returns the response component.
- getRuntimePath() : string
- Returns the directory that stores runtime files.
- getSecurity() : Security
- Returns the security component.
- getTimeZone() : string
- Returns the time zone used by this application.
- getUniqueId() : string
- Returns an ID that uniquely identifies this module among all modules within the current application.
- getUrlManager() : UrlManager
- Returns the URL manager for this application.
- getVendorPath() : string
- Returns the directory that stores vendor files.
- getVersion() : string
- Returns current module version.
- getView() : View|View
- Returns the view object.
- getViewPath() : string
- Returns the directory that contains the view files for this module.
- handleRequest() : Response
- Handles the specified request.
- has() : bool
- Returns a value indicating whether the locator has the specified component definition or has instantiated the component.
- hasEventHandlers() : bool
- Returns a value indicating whether there is any handler attached to the named event.
- hasMethod() : bool
- Returns a value indicating whether a method is defined.
- hasModule() : bool
- Checks whether the child module of the specified ID exists.
- hasProperty() : bool
- Returns a value indicating whether a property is defined for this component.
- init() : mixed
- Initializes the module.
- off() : bool
- Detaches an existing event handler from this component.
- on() : mixed
- Attaches an event handler to an event.
- preInit() : mixed
- Pre-initializes the application.
- run() : int
- Runs the application.
- runAction() : mixed
- Runs a controller action specified by a route.
- set() : mixed
- Registers a component definition with this locator.
- setAliases() : mixed
- Defines path aliases.
- setBasePath() : mixed
- Sets the root directory of the application and the @app alias.
- setComponents() : mixed
- Registers a set of component definitions in this locator.
- setContainer() : mixed
- Configures [[Yii::$container]] with the $config.
- setControllerPath() : mixed
- Sets the directory that contains the controller classes.
- setInstance() : mixed
- Sets the currently requested instance of this module class.
- setLayoutPath() : mixed
- Sets the directory that contains the layout files.
- setModule() : mixed
- Adds a sub-module to this module.
- setModules() : mixed
- Registers sub-modules in the current module.
- setRuntimePath() : mixed
- Sets the directory that stores runtime files.
- setTimeZone() : mixed
- Sets the time zone used by this application.
- setVendorPath() : mixed
- Sets the directory that stores vendor files.
- setVersion() : mixed
- Sets current module version.
- setViewPath() : mixed
- Sets the directory that contains the view files.
- trigger() : mixed
- Triggers an event.
- bootstrap() : mixed
- Initializes extensions and executes bootstrap components.
- defaultVersion() : string
- Returns default module version.
- registerErrorHandler() : mixed
- Registers the errorHandler component as a PHP error handler.
- attachBehaviorInternal() : Behavior
- Attaches a behavior to this component.
- isIncorrectClassNameOrPrefix() : bool
- Checks if class name or prefix is incorrect
Constants
EVENT_AFTER_ACTION
public
mixed
EVENT_AFTER_ACTION
= 'afterAction'
Tags
EVENT_AFTER_REQUEST
public
mixed
EVENT_AFTER_REQUEST
= 'afterRequest'
Tags
EVENT_BEFORE_ACTION
public
mixed
EVENT_BEFORE_ACTION
= 'beforeAction'
Tags
EVENT_BEFORE_REQUEST
public
mixed
EVENT_BEFORE_REQUEST
= 'beforeRequest'
Tags
STATE_AFTER_REQUEST
Application state used by [[state]]: application is triggering [[EVENT_AFTER_REQUEST]].
public
mixed
STATE_AFTER_REQUEST
= 4
.
STATE_BEFORE_REQUEST
Application state used by [[state]]: application is triggering [[EVENT_BEFORE_REQUEST]].
public
mixed
STATE_BEFORE_REQUEST
= 2
STATE_BEGIN
Application state used by [[state]]: application just started.
public
mixed
STATE_BEGIN
= 0
STATE_END
Application state used by [[state]]: application has ended.
public
mixed
STATE_END
= 6
STATE_HANDLING_REQUEST
Application state used by [[state]]: application is handling the request.
public
mixed
STATE_HANDLING_REQUEST
= 3
STATE_INIT
Application state used by [[state]]: application is initializing.
public
mixed
STATE_INIT
= 1
STATE_SENDING_RESPONSE
Application state used by [[state]]: application is about to send response.
public
mixed
STATE_SENDING_RESPONSE
= 5
Properties
$aliases write-only
public
array<string|int, mixed>
$aliases
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.
$assetManager read-only
public
AssetManager
$assetManager
The asset manager application component.
$authManager read-only
public
ManagerInterface|null
$authManager
The auth manager application component or null if it's not configured.
$basePath
public
string
$basePath
The root directory of the module.
$basePath
public
string
$basePath
The root directory of the application.
$behaviors read-only
public
array<string|int, Behavior>
$behaviors
List of behaviors attached to this component.
$bootstrap
public
array<string|int, mixed>
$bootstrap
= []
list of components that should be run during the application [[bootstrap()|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 [[BootstrapInterface::bootstrap()|bootstrap()]] method will be also be called.
$cache read-only
public
CacheInterface|null
$cache
The cache application component. Null if the component is not enabled.
$charset
public
string
$charset
= 'UTF-8'
the charset currently used for the application.
$components
public
array<string|int, mixed>
$components
The list of the component definitions or the loaded component instances (ID => definition or instance).
$container write-only
public
array<string|int, mixed>
$container
Values given in terms of name-value pairs.
$controller
public
Controller
$controller
the currently active controller instance
$controllerMap
public
array<string|int, mixed>
$controllerMap
= []
mapping from controller ID to controller configurations.
Each name-value pair specifies the configuration of a single controller.
A controller configuration can be either a string or an array.
If the former, the string should be the fully qualified class name of the controller.
If the latter, the array must contain a class
element which specifies
the controller's fully qualified class name, and the rest of the name-value pairs
in the array are used to initialize the corresponding controller properties. For example,
[
'account' => 'app\controllers\UserController',
'article' => [
'class' => 'app\controllers\PostController',
'pageTitle' => 'something new',
],
]
$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.
$controllerPath
public
string
$controllerPath
The directory that contains the controller classes.
$db read-only
public
Connection
$db
The database connection.
$defaultRoute
public
string
$defaultRoute
= 'default'
the default route of this module. Defaults to default
.
The route may consist of child module ID, controller ID, and/or action ID.
For example, help
, post/create
, admin/post/create
.
If action ID is not given, it will take the default value as specified in
[[Controller::defaultAction]].
$errorHandler read-only
public
ErrorHandler|ErrorHandler
$errorHandler
The error handler application component.
$extensions
public
array<string|int, mixed>|null
$extensions
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 [[bootstrap()|bootstrapping process]]. If the class implements [[BootstrapInterface]], its [[BootstrapInterface::bootstrap()|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
.
$formatter read-only
public
Formatter
$formatter
The formatter application component.
$i18n read-only
public
I18N
$i18n
The internationalization application component.
$id
public
string
$id
an ID that uniquely identifies this module among other modules which have the same [[module|parent]].
$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).
Tags
$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.
$layoutPath
public
string
$layoutPath
The root directory of layout files. Defaults to "[[viewPath]]/layouts".
$loadedModules
public
array<string|int, mixed>
$loadedModules
= []
list of loaded modules indexed by their class names.
$log read-only
public
Dispatcher
$log
The log dispatcher application component.
$mailer read-only
public
MailerInterface
$mailer
The mailer application component.
$module
public
Module|null
$module
the parent module of this module. null
if this module does not have a parent.
$modules
public
array<string|int, mixed>
$modules
The modules (indexed by their IDs).
$name
public
string
$name
= 'My Application'
the application name.
$params
public
array<string|int, mixed>
$params
= []
custom module parameters (name => value).
$request read-only
public
Request|Request
$request
The request component.
$requestedAction
public
Action|null
$requestedAction
the requested Action. If null, it means the request cannot be resolved into an action.
$requestedParams
public
array<string|int, mixed>
$requestedParams
the parameters supplied to the requested action.
$requestedRoute
public
string
$requestedRoute
the requested route
$response read-only
public
Response|Response
$response
The response component.
$runtimePath
public
string
$runtimePath
The directory that stores runtime files. Defaults to the "runtime" subdirectory under [[basePath]].
$security read-only
public
Security
$security
The security application component.
$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.
Tags
$state
public
int
$state
the current application state during a request handling life cycle. This property is managed by the application. Do not modify this property.
$timeZone
public
string
$timeZone
The time zone used by this application.
$uniqueId read-only
public
string
$uniqueId
The unique ID of the module.
$uniqueId read-only
public
string
$uniqueId
The unique ID of the module.
$urlManager read-only
public
UrlManager
$urlManager
The URL manager for this application.
$vendorPath
public
string
$vendorPath
The directory that stores vendor files. Defaults to "vendor" directory under [[basePath]].
$version
public
string
$version
The version of this module. Note that the type of this property differs in getter and setter. See [[getVersion()]] and [[setVersion()]] for details.
$view read-only
public
View|View
$view
The view application component that is used to render various view files.
$viewPath
public
string
$viewPath
The root directory of view files. Defaults to "[[basePath]]/views".
$_basePath
private
string
$_basePath
the root directory of the module.
$_behaviors
private
array<string|int, Behavior>|null
$_behaviors
the attached behaviors (behavior name => behavior). This is null
when not initialized.
$_components
private
array<string|int, mixed>
$_components
= []
shared component instances indexed by their IDs
$_controllerPath
private
string
$_controllerPath
The root directory that contains the controller classes for this module.
$_definitions
private
array<string|int, mixed>
$_definitions
= []
component definitions indexed by their IDs
$_events
private
array<string|int, mixed>
$_events
= []
the attached event handlers (event name => handlers)
$_eventWildcards
private
array<string|int, mixed>
$_eventWildcards
= []
the event handlers attached for wildcard patterns (event name wildcard => handlers)
Tags
$_layoutPath
private
string
$_layoutPath
the root directory that contains layout view files for this module.
$_modules
private
array<string|int, mixed>
$_modules
= []
child modules of this module
$_runtimePath
private
mixed
$_runtimePath
$_vendorPath
private
mixed
$_vendorPath
$_version
private
string|callable|null
$_version
the version of this module. Version can be specified as a PHP callback, which can accept module instance as an argument and should return the actual version. For example:
function (Module $module) {
//return string|int
}
If not set, [[defaultVersion()]] will be used to determine actual value.
Tags
$_viewPath
private
string
$_viewPath
the root directory that contains view files for this module
Methods
__call()
Calls the named method which is not a class method.
public
__call(string $name, array<string|int, mixed> $params) : mixed
This method will check if any attached behavior has the named method and will execute it if available.
Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.
Parameters
- $name : string
-
the method name
- $params : array<string|int, mixed>
-
method parameters
Tags
Return values
mixed —the method return value
__clone()
This method is called after the object is created by cloning an existing one.
public
__clone() : mixed
It removes all behaviors because they are attached to the old object.
__construct()
Constructor.
public
__construct([array<string|int, mixed> $config = [] ]) : mixed
Parameters
- $config : array<string|int, mixed> = []
-
name-value pairs that will be used to initialize the object properties. Note that the configuration must contain both [[id]] and [[basePath]].
Tags
__get()
Getter magic method.
public
__get(string $name) : mixed
This method is overridden to support accessing components like reading properties.
Parameters
- $name : string
-
component or property name
Return values
mixed —the named property value
__isset()
Checks if a property value is null.
public
__isset(string $name) : bool
This method overrides the parent implementation by checking if the named component is loaded.
Parameters
- $name : string
-
the property name or the event name
Return values
bool —whether the property value is null
__set()
Sets the value of a component property.
public
__set(string $name, mixed $value) : mixed
This method will check in the following order and act accordingly:
- a property defined by a setter: set the property value
- an event in the format of "on xyz": attach the handler to the event "xyz"
- a behavior in the format of "as xyz": attach the behavior named as "xyz"
- a property of a behavior: set the behavior property value
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing $component->property = $value;
.
Parameters
- $name : string
-
the property name or the event name
- $value : mixed
-
the property value
Tags
__unset()
Sets a component property to be null.
public
__unset(string $name) : mixed
This method will check in the following order and act accordingly:
- a property defined by a setter: set the property value to be null
- a property of a behavior: set the property value to be null
Do not call this method directly as it is a PHP magic method that
will be implicitly called when executing unset($component->property)
.
Parameters
- $name : string
-
the property name
Tags
afterAction()
This method is invoked right after an action within this module is executed.
public
afterAction(Action $action, mixed $result) : mixed
The method will trigger the [[EVENT_AFTER_ACTION]] event. The return value of the method will be used as the action return value.
If you override this method, your code should look like the following:
public function afterAction($action, $result)
{
$result = parent::afterAction($action, $result);
// your custom code here
return $result;
}
Parameters
- $action : Action
-
the action just executed.
- $result : mixed
-
the action return result.
Return values
mixed —the processed action result.
attachBehavior()
Attaches a behavior to this component.
public
attachBehavior(string $name, string|array<string|int, mixed>|Behavior $behavior) : Behavior
This method will create the behavior object based on the given configuration. After that, the behavior object will be attached to this component by calling the [[Behavior::attach()]] method.
Parameters
- $name : string
-
the name of the behavior.
- $behavior : string|array<string|int, mixed>|Behavior
-
the behavior configuration. This can be one of the following:
- a [[Behavior]] object
- a string specifying the behavior class
- an object configuration array that will be passed to [[Yii::createObject()]] to create the behavior object.
Tags
Return values
Behavior —the behavior object
attachBehaviors()
Attaches a list of behaviors to the component.
public
attachBehaviors(array<string|int, mixed> $behaviors) : mixed
Each behavior is indexed by its name and should be a [[Behavior]] object, a string specifying the behavior class, or an configuration array for creating the behavior.
Parameters
- $behaviors : array<string|int, mixed>
-
list of behaviors to be attached to the component
Tags
beforeAction()
This method is invoked right before an action within this module is executed.
public
beforeAction(Action $action) : bool
The method will trigger the [[EVENT_BEFORE_ACTION]] event. The return value of the method will determine whether the action should continue to run.
In case the action should not run, the request should be handled inside of the beforeAction
code
by either providing the necessary output or redirecting the request. Otherwise the response will be empty.
If you override this method, your code should look like the following:
public function beforeAction($action)
{
if (!parent::beforeAction($action)) {
return false;
}
// your custom code here
return true; // or false to not run the action
}
Parameters
- $action : Action
-
the action to be executed.
Return values
bool —whether the action should continue to be executed.
behaviors()
Returns a list of behaviors that this component should behave as.
public
behaviors() : array<string|int, mixed>
Child classes may override this method to specify the behaviors they want to behave as.
The return value of this method should be an array of behavior objects or configurations indexed by behavior names. A behavior configuration can be either a string specifying the behavior class or an array of the following structure:
'behaviorName' => [
'class' => 'BehaviorClass',
'property1' => 'value1',
'property2' => 'value2',
]
Note that a behavior class must extend from [[Behavior]]. Behaviors can be attached using a name or anonymously. When a name is used as the array key, using this name, the behavior can later be retrieved using [[getBehavior()]] or be detached using [[detachBehavior()]]. Anonymous behaviors can not be retrieved or detached.
Behaviors declared in this method will be attached to the component automatically (on demand).
Return values
array<string|int, mixed> —the behavior configurations.
canGetProperty()
Returns a value indicating whether a property can be read.
public
canGetProperty(string $name[, bool $checkVars = true ][, bool $checkBehaviors = true ]) : bool
A property can be read if:
- the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true); - an attached behavior has a readable property of the given name (when
$checkBehaviors
is true).
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
- $checkBehaviors : bool = true
-
whether to treat behaviors' properties as properties of this component
Tags
Return values
bool —whether the property can be read
canSetProperty()
Returns a value indicating whether a property can be set.
public
canSetProperty(string $name[, bool $checkVars = true ][, bool $checkBehaviors = true ]) : bool
A property can be written if:
- the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true); - an attached behavior has a writable property of the given name (when
$checkBehaviors
is true).
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
- $checkBehaviors : bool = true
-
whether to treat behaviors' properties as properties of this component
Tags
Return values
bool —whether the property can be written
className()
Returns the fully qualified name of this class.
public
static className() : string
Tags
Return values
string —the fully qualified name of this class.
clear()
Removes the component from the locator.
public
clear(string $id) : mixed
Parameters
- $id : string
-
the component ID
coreComponents()
Returns the configuration of core application components.
public
coreComponents() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed>createController()
Creates a controller instance based on the given route.
public
createController(string $route) : array<string|int, mixed>|bool
The route should be relative to this module. The method implements the following algorithm to resolve the given route:
- If the route is empty, use [[defaultRoute]];
- If the first segment of the route is found in [[controllerMap]], create a controller based on the corresponding configuration found in [[controllerMap]];
- If the first segment of the route is a valid module ID as declared in [[modules]],
call the module's
createController()
with the rest part of the route; - The given route is in the format of
abc/def/xyz
. Try eitherabc\DefController
orabc\def\XyzController
class within the [[controllerNamespace|controller namespace]].
If any of the above steps resolves into a controller, it is returned together with the rest
part of the route which will be treated as the action ID. Otherwise, false
will be returned.
Parameters
- $route : string
-
the route consisting of module, controller and action IDs.
Tags
Return values
array<string|int, mixed>|bool —If the controller is created successfully, it will be returned together
with the requested action ID. Otherwise false
will be returned.
createControllerByID()
Creates a controller based on the given controller ID.
public
createControllerByID(string $id) : Controller|null
The controller ID is relative to this module. The controller class should be namespaced under [[controllerNamespace]].
Note that this method does not check [[modules]] or [[controllerMap]].
Parameters
- $id : string
-
the controller ID.
Tags
Return values
Controller|null —the newly created controller instance, or null
if the controller ID is invalid.
detachBehavior()
Detaches a behavior from the component.
public
detachBehavior(string $name) : Behavior|null
The behavior's [[Behavior::detach()]] method will be invoked.
Parameters
- $name : string
-
the behavior's name.
Return values
Behavior|null —the detached behavior. Null if the behavior does not exist.
detachBehaviors()
Detaches all behaviors from the component.
public
detachBehaviors() : mixed
end()
Terminates the application.
public
end([int $status = 0 ][, Response|null $response = null ]) : mixed
This method replaces the exit()
function by ensuring the application life cycle is completed
before terminating the application.
Parameters
- $status : int = 0
-
the exit status (value 0 means normal exit while other values mean abnormal exit).
- $response : Response|null = null
-
the response to be sent. If not set, the default application [[response]] component will be used.
Tags
ensureBehaviors()
Makes sure that the behaviors declared in [[behaviors()]] are attached to this component.
public
ensureBehaviors() : mixed
get()
Returns the component instance with the specified ID.
public
get(mixed $id[, mixed $throwException = true ]) : object|null
Since version 2.0.13, if a component isn't defined in the module, it will be looked up in the parent module. The parent module may be the application.
Parameters
- $id : mixed
-
component ID (e.g.
db
). - $throwException : mixed = true
-
whether to throw an exception if
$id
is not registered with the locator before.
Return values
object|null —the component of the specified ID. If $throwException
is false and $id
is not registered before, null will be returned.
getAssetManager()
Returns the asset manager.
public
getAssetManager() : AssetManager
Return values
AssetManager —the asset manager application component.
getAuthManager()
Returns the auth manager for this application.
public
getAuthManager() : ManagerInterface|null
Return values
ManagerInterface|null —the auth manager application component or null if it's not configured.
getBasePath()
Returns the root directory of the module.
public
getBasePath() : string
It defaults to the directory containing the module class file.
Return values
string —the root directory of the module.
getBehavior()
Returns the named behavior object.
public
getBehavior(string $name) : Behavior|null
Parameters
- $name : string
-
the behavior name
Return values
Behavior|null —the behavior object, or null if the behavior does not exist
getBehaviors()
Returns all behaviors attached to this component.
public
getBehaviors() : array<string|int, Behavior>
Return values
array<string|int, Behavior> —list of behaviors attached to this component
getCache()
Returns the cache component.
public
getCache() : CacheInterface|null
Return values
CacheInterface|null —the cache application component. Null if the component is not enabled.
getComponents()
Returns the list of the component definitions or the loaded component instances.
public
getComponents([bool $returnDefinitions = true ]) : array<string|int, mixed>
Parameters
- $returnDefinitions : bool = true
-
whether to return component definitions instead of the loaded component instances.
Return values
array<string|int, mixed> —the list of the component definitions or the loaded component instances (ID => definition or instance).
getControllerPath()
Returns the directory that contains the controller classes according to [[controllerNamespace]].
public
getControllerPath() : string
Note that in order for this method to return a value, you must define an alias for the root namespace of [[controllerNamespace]].
Tags
Return values
string —the directory that contains the controller classes.
getDb()
Returns the database connection component.
public
getDb() : Connection
Return values
Connection —the database connection.
getErrorHandler()
Returns the error handler component.
public
getErrorHandler() : ErrorHandler|ErrorHandler
Return values
ErrorHandler|ErrorHandler —the error handler application component.
getFormatter()
Returns the formatter component.
public
getFormatter() : Formatter
Return values
Formatter —the formatter application component.
getI18n()
Returns the internationalization (i18n) component.
public
getI18n() : I18N
Return values
I18N —the internationalization application component.
getInstance()
Returns the currently requested instance of this module class.
public
static getInstance() : static|null
If the module class is not currently requested, null
will be returned.
This method is provided so that you access the module instance from anywhere within the module.
Return values
static|null —the currently requested instance of this module class, or null
if the module class is not requested.
getLayoutPath()
Returns the directory that contains layout view files for this module.
public
getLayoutPath() : string
Return values
string —the root directory of layout files. Defaults to "[[viewPath]]/layouts".
getLog()
Returns the log dispatcher component.
public
getLog() : Dispatcher
Return values
Dispatcher —the log dispatcher application component.
getMailer()
Returns the mailer component.
public
getMailer() : MailerInterface
Tags
Return values
MailerInterface —the mailer application component.
getModule()
Retrieves the child module of the specified ID.
public
getModule(string $id[, bool $load = true ]) : Module|null
This method supports retrieving both child modules and grand child modules.
Parameters
- $id : string
-
module ID (case-sensitive). To retrieve grand child modules, use ID path relative to this module (e.g.
admin/content
). - $load : bool = true
-
whether to load the module if it is not yet loaded.
Tags
Return values
Module|null —the module instance, null
if the module does not exist.
getModules()
Returns the sub-modules in this module.
public
getModules([bool $loadedOnly = false ]) : array<string|int, mixed>
Parameters
- $loadedOnly : bool = false
-
whether to return the loaded sub-modules only. If this is set
false
, then all sub-modules registered in this module will be returned, whether they are loaded or not. Loaded modules will be returned as objects, while unloaded modules as configuration arrays.
Return values
array<string|int, mixed> —the modules (indexed by their IDs).
getRequest()
Returns the request component.
public
getRequest() : Request|Request
Return values
Request|Request —the request component.
getResponse()
Returns the response component.
public
getResponse() : Response|Response
Return values
Response|Response —the response component.
getRuntimePath()
Returns the directory that stores runtime files.
public
getRuntimePath() : string
Return values
string —the directory that stores runtime files. Defaults to the "runtime" subdirectory under [[basePath]].
getSecurity()
Returns the security component.
public
getSecurity() : Security
Return values
Security —the security application component.
getTimeZone()
Returns the time zone used by this application.
public
getTimeZone() : string
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.
Tags
Return values
string —the time zone used by this application.
getUniqueId()
Returns an ID that uniquely identifies this module among all modules within the current application.
public
getUniqueId() : string
Since this is an application instance, it will always return an empty string.
Return values
string —the unique ID of the module.
getUrlManager()
Returns the URL manager for this application.
public
getUrlManager() : UrlManager
Return values
UrlManager —the URL manager for this application.
getVendorPath()
Returns the directory that stores vendor files.
public
getVendorPath() : string
Return values
string —the directory that stores vendor files. Defaults to "vendor" directory under [[basePath]].
getVersion()
Returns current module version.
public
getVersion() : string
If version is not explicitly set, [[defaultVersion()]] method will be used to determine its value.
Tags
Return values
string —the version of this module.
getView()
Returns the view object.
public
getView() : View|View
Return values
View|View —the view application component that is used to render various view files.
getViewPath()
Returns the directory that contains the view files for this module.
public
getViewPath() : string
Return values
string —the root directory of view files. Defaults to "[[basePath]]/views".
handleRequest()
Handles the specified request.
public
abstract handleRequest(Request $request) : Response
This method should return an instance of [[Response]] or its child class which represents the handling result of the request.
Parameters
- $request : Request
-
the request to be handled
Return values
Response —the resulting response
has()
Returns a value indicating whether the locator has the specified component definition or has instantiated the component.
public
has(mixed $id[, mixed $checkInstance = false ]) : bool
Since version 2.0.13, if a component isn't defined in the module, it will be looked up in the parent module. The parent module may be the application.
Parameters
- $id : mixed
-
component ID (e.g.
db
). - $checkInstance : mixed = false
-
whether the method should check if the component is shared and instantiated.
Return values
bool —whether the locator has the specified component definition or has instantiated the component.
hasEventHandlers()
Returns a value indicating whether there is any handler attached to the named event.
public
hasEventHandlers(string $name) : bool
Parameters
- $name : string
-
the event name
Return values
bool —whether there is any handler attached to the event.
hasMethod()
Returns a value indicating whether a method is defined.
public
hasMethod(string $name[, bool $checkBehaviors = true ]) : bool
A method is defined if:
- the class has a method with the specified name
- an attached behavior has a method with the given name (when
$checkBehaviors
is true).
Parameters
- $name : string
-
the property name
- $checkBehaviors : bool = true
-
whether to treat behaviors' methods as methods of this component
Return values
bool —whether the method is defined
hasModule()
Checks whether the child module of the specified ID exists.
public
hasModule(string $id) : bool
This method supports checking the existence of both child and grand child modules.
Parameters
- $id : string
-
module ID. For grand child modules, use ID path relative to this module (e.g.
admin/content
).
Return values
bool —whether the named module exists. Both loaded and unloaded modules are considered.
hasProperty()
Returns a value indicating whether a property is defined for this component.
public
hasProperty(string $name[, bool $checkVars = true ][, bool $checkBehaviors = true ]) : bool
A property is defined if:
- the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
- the class has a member variable with the specified name (when
$checkVars
is true); - an attached behavior has a property of the given name (when
$checkBehaviors
is true).
Parameters
- $name : string
-
the property name
- $checkVars : bool = true
-
whether to treat member variables as properties
- $checkBehaviors : bool = true
-
whether to treat behaviors' properties as properties of this component
Tags
Return values
bool —whether the property is defined
init()
Initializes the module.
public
init() : mixed
off()
Detaches an existing event handler from this component.
public
off(string $name[, callable|null $handler = null ]) : bool
This method is the opposite of [[on()]].
Note: in case wildcard pattern is passed for event name, only the handlers registered with this wildcard will be removed, while handlers registered with plain names matching this wildcard will remain.
Parameters
- $name : string
-
event name
- $handler : callable|null = null
-
the event handler to be removed. If it is null, all handlers attached to the named event will be removed.
Tags
Return values
bool —if a handler is found and detached
on()
Attaches an event handler to an event.
public
on(string $name, callable $handler[, mixed $data = null ][, bool $append = true ]) : mixed
The event handler must be a valid PHP callback. The following are some examples:
function ($event) { ... } // anonymous function
[$object, 'handleClick'] // $object->handleClick()
['Page', 'handleClick'] // Page::handleClick()
'handleClick' // global function handleClick()
The event handler must be defined with the following signature,
function ($event)
where $event
is an [[Event]] object which includes parameters associated with the event.
Since 2.0.14 you can specify event name as a wildcard pattern:
$component->on('event.group.*', function ($event) {
Yii::trace($event->name . ' is triggered.');
});
Parameters
- $name : string
-
the event name
- $handler : callable
-
the event handler
- $data : mixed = null
-
the data to be passed to the event handler when the event is triggered. When the event handler is invoked, this data can be accessed via [[Event::data]].
- $append : bool = true
-
whether to append new event handler to the end of the existing handler list. If false, the new handler will be inserted at the beginning of the existing handler list.
Tags
preInit()
Pre-initializes the application.
public
preInit(array<string|int, mixed> &$config) : mixed
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
- $config : array<string|int, mixed>
-
the application configuration
Tags
run()
Runs the application.
public
run() : int
This is the main entrance of an application.
Return values
int —the exit status (0 means normal, non-zero values mean abnormal)
runAction()
Runs a controller action specified by a route.
public
runAction(string $route[, array<string|int, mixed> $params = [] ]) : mixed
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]].
Parameters
- $route : string
-
the route that specifies the action.
- $params : array<string|int, mixed> = []
-
the parameters to be passed to the action
Tags
Return values
mixed —the result of the action.
set()
Registers a component definition with this locator.
public
set(string $id, mixed $definition) : mixed
For example,
// a class name
$locator->set('cache', 'yii\caching\FileCache');
// a configuration array
$locator->set('db', [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=demo',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
]);
// an anonymous function
$locator->set('cache', function ($params) {
return new \yii\caching\FileCache;
});
// an instance
$locator->set('cache', new \yii\caching\FileCache);
If a component definition with the same ID already exists, it will be overwritten.
Parameters
- $id : string
-
component ID (e.g.
db
). - $definition : mixed
-
the component definition to be registered with this locator. It can be one of the following:
- a class name
- a configuration array: the array contains name-value pairs that will be used to
initialize the property values of the newly created object when [[get()]] is called.
The
class
element is required and stands for the the class of the object to be created. - a PHP callable: either an anonymous function or an array representing a class method (e.g.
['Foo', 'bar']
). The callable will be called by [[get()]] to return an object associated with the specified component ID. - an object: When [[get()]] is called, this object will be returned.
Tags
setAliases()
Defines path aliases.
public
setAliases(array<string|int, mixed> $aliases) : mixed
This method calls [[Yii::setAlias()]] to register the path aliases. This method is provided so that you can define path aliases when configuring a module.
Parameters
- $aliases : array<string|int, mixed>
-
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. For example,[ '@models' => '@app/models', // an existing alias '@backend' => __DIR__ . '/../backend', // a directory ]
Tags
setBasePath()
Sets the root directory of the application and the @app alias.
public
setBasePath(string $path) : mixed
This method can only be invoked at the beginning of the constructor.
Parameters
- $path : string
-
the root directory of the application.
Tags
setComponents()
Registers a set of component definitions in this locator.
public
setComponents(array<string|int, mixed> $components) : mixed
This is the bulk version of [[set()]]. The parameter should be an array whose keys are component IDs and values the corresponding component definitions.
For more details on how to specify component IDs and definitions, please refer to [[set()]].
If a component definition with the same ID already exists, it will be overwritten.
The following is an example for registering two component definitions:
[
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'sqlite:path/to/file.db',
],
'cache' => [
'class' => 'yii\caching\DbCache',
'db' => 'db',
],
]
Parameters
- $components : array<string|int, mixed>
-
component definitions or instances
setContainer()
Configures [[Yii::$container]] with the $config.
public
setContainer(array<string|int, mixed> $config) : mixed
Parameters
- $config : array<string|int, mixed>
-
values given in terms of name-value pairs
Tags
setControllerPath()
Sets the directory that contains the controller classes.
public
setControllerPath(string $path) : mixed
Parameters
- $path : string
-
the root directory that contains the controller classes.
Tags
setInstance()
Sets the currently requested instance of this module class.
public
static setInstance(Module|null $instance) : mixed
Parameters
- $instance : Module|null
-
the currently requested instance of this module class. If it is
null
, the instance of the calling class will be removed, if any.
setLayoutPath()
Sets the directory that contains the layout files.
public
setLayoutPath(string $path) : mixed
Parameters
- $path : string
-
the root directory or path alias of layout files.
Tags
setModule()
Adds a sub-module to this module.
public
setModule(string $id, Module|array<string|int, mixed>|null $module) : mixed
Parameters
- $id : string
-
module ID.
- $module : Module|array<string|int, mixed>|null
-
the sub-module to be added to this module. This can be one of the following:
- a [[Module]] object
- a configuration array: when [[getModule()]] is called initially, the array will be used to instantiate the sub-module
-
null
: the named sub-module will be removed from this module
setModules()
Registers sub-modules in the current module.
public
setModules(array<string|int, mixed> $modules) : mixed
Each sub-module should be specified as a name-value pair, where name refers to the ID of the module and value the module or a configuration array that can be used to create the module. In the latter case, [[Yii::createObject()]] will be used to create the module.
If a new sub-module has the same ID as an existing one, the existing one will be overwritten silently.
The following is an example for registering two sub-modules:
[
'comment' => [
'class' => 'app\modules\comment\CommentModule',
'db' => 'db',
],
'booking' => ['class' => 'app\modules\booking\BookingModule'],
]
Parameters
- $modules : array<string|int, mixed>
-
modules (id => module configuration or instances).
setRuntimePath()
Sets the directory that stores runtime files.
public
setRuntimePath(string $path) : mixed
Parameters
- $path : string
-
the directory that stores runtime files.
setTimeZone()
Sets the time zone used by this application.
public
setTimeZone(string $value) : mixed
This is a simple wrapper of PHP function date_default_timezone_set(). Refer to the php manual for available timezones.
Parameters
- $value : string
-
the time zone used by this application.
Tags
setVendorPath()
Sets the directory that stores vendor files.
public
setVendorPath(string $path) : mixed
Parameters
- $path : string
-
the directory that stores vendor files.
setVersion()
Sets current module version.
public
setVersion(string|callable|null $version) : mixed
Parameters
- $version : string|callable|null
-
the version of this module. Version can be specified as a PHP callback, which can accept module instance as an argument and should return the actual version. For example:
function (Module $module) { //return string }
Tags
setViewPath()
Sets the directory that contains the view files.
public
setViewPath(string $path) : mixed
Parameters
- $path : string
-
the root directory of view files.
Tags
trigger()
Triggers an event.
public
trigger(string $name[, Event|null $event = null ]) : mixed
This method represents the happening of an event. It invokes all attached handlers for the event including class-level handlers.
Parameters
- $name : string
-
the event name
- $event : Event|null = null
-
the event instance. If not set, a default [[Event]] object will be created.
bootstrap()
Initializes extensions and executes bootstrap components.
protected
bootstrap() : mixed
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.
defaultVersion()
Returns default module version.
protected
defaultVersion() : string
Child class may override this method to provide more specific version detection.
Tags
Return values
string —the version of this module.
registerErrorHandler()
Registers the errorHandler component as a PHP error handler.
protected
registerErrorHandler(array<string|int, mixed> &$config) : mixed
Parameters
- $config : array<string|int, mixed>
-
application config
attachBehaviorInternal()
Attaches a behavior to this component.
private
attachBehaviorInternal(string|int $name, string|array<string|int, mixed>|Behavior $behavior) : Behavior
Parameters
- $name : string|int
-
the name of the behavior. If this is an integer, it means the behavior is an anonymous one. Otherwise, the behavior is a named one and any existing behavior with the same name will be detached first.
- $behavior : string|array<string|int, mixed>|Behavior
-
the behavior to be attached
Return values
Behavior —the attached behavior.
isIncorrectClassNameOrPrefix()
Checks if class name or prefix is incorrect
private
isIncorrectClassNameOrPrefix(string $className, string $prefix) : bool
Parameters
- $className : string
- $prefix : string