Application
API reference › yii › web › Application
1 subclass
Application is the base class for all web 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. | yii\ |
$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. | yii\ |
$aliases write-only | array | List of path aliases to be defined. | yii\ |
$assetManager read-only | AssetManager | The asset manager application component. | yii\ |
$authManager read-only | yii\| | The auth manager application component or null if it’s not configured. | yii\ |
$basePath | string | The root directory of the application. | yii\ |
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$bootstrap | array | List of components that should be run during the application bootstrapping process. Default: []. | yii\ |
$cache read-only | yii\| | The cache application component. | yii\ |
$catchAll | array| | The configuration specifying a controller action which should handle all user requests. Default: null. | Application |
$charset | string | The charset currently used for the application. Default: 'UTF-8'. | yii\ |
$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. | yii\ |
$controller | Controller| | The currently active controller instance Default: null. | Application |
$controllerMap | array | Mapping from controller ID to controller configurations. Default: []. | yii\ |
$controllerNamespace | string | The namespace that controller classes are located in. Default: 'app\. | yii\ |
$controllerPath | string | The directory that contains the controller classes. | yii\ |
$db read-only | yii\ | The database connection. | yii\ |
$defaultRoute | string | The default route of this application. Default: 'site'. | Application |
$errorHandler read-only | ErrorHandler | The error handler application component. | Application |
$extensions | array| | List of installed Yii extensions. Default: null. | yii\ |
$formatter read-only | yii\ | The formatter application component. | yii\ |
$homeUrl | string | The homepage URL. | Application |
$i18n read-only | yii\ | The internationalization application component. | yii\ |
$id | string | An ID that uniquely identifies this module among other modules which have the same parent. Default: null. | yii\ |
$language | string | The language that is meant to be used for end users. Default: 'en-US'. | yii\ |
$layout | string| | The layout that should be applied for views in this application. Default: 'main'. | yii\ |
$layoutPath | string | The root directory of layout files. | yii\ |
$loadedModules | array | List of loaded modules indexed by their class names. Default: []. | yii\ |
$log read-only | yii\ | The log dispatcher application component. | yii\ |
$mailer read-only | yii\ | The mailer application component. | yii\ |
$module | yii\| | The parent module of this module. null if this module does not have a parent. Default: null. | yii\ |
$modules | array | The modules (indexed by their IDs). | yii\ |
$name | string | The application name. Default: 'My Application'. | yii\ |
$params | array | Custom module parameters (name => value). Default: []. | yii\ |
$request read-only | Request | The request component. | Application |
$requestedAction | yii\<yii\>| | The requested Action. Default: null. | yii\ |
$requestedParams | array| | The parameters supplied to the requested action. Default: null. | yii\ |
$requestedRoute | string | The requested route Default: null. | yii\ |
$response read-only | Response | The response component. | Application |
$runtimePath | string | The directory that stores runtime files. | yii\ |
$security read-only | yii\ | The security application component. | yii\ |
$session read-only | Session | The session component. | Application |
$sourceLanguage | string | The language that the application is written in. Default: 'en-US'. | yii\ |
$state | int | The current application state during a request handling life cycle. Default: null. | yii\ |
$timeZone | string | The time zone used by this application. | yii\ |
$uniqueId read-only | string | The unique ID of the module. | yii\ |
$urlManager read-only | UrlManager | The URL manager for this application. | yii\ |
$user read-only | User<yii\ | The user component. | Application |
$vendorPath | string | The directory that stores vendor files. | yii\ |
$version | string| | The version of this module. | yii\ |
$view read-only | yii\|View | The view application component that is used to render various view files. | yii\ |
$viewPath | string | The root directory of view files. | yii\ |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Getter magic method. | yii\ |
__isset() | Checks if a property value is null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
afterAction() | This method is invoked right after an action within this module is executed. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
beforeAction() | This method is invoked right before an action within this module is executed. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
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. | yii\ |
createControllerByID() | Creates a controller based on the given controller ID. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
end() | Terminates the application. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
get() | Returns the component instance with the specified ID. | yii\ |
getAssetManager() | Returns the asset manager. | yii\ |
getAuthManager() | Returns the auth manager for this application. | yii\ |
getBasePath() | Returns the root directory of the module. | yii\ |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
getCache() | Returns the cache component. | yii\ |
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. | yii\ |
getDb() | Returns the database connection component. | yii\ |
getErrorHandler() | Returns the error handler component. | Application |
getFormatter() | Returns the formatter component. | yii\ |
getHomeUrl() | Application | |
getI18n() | Returns the internationalization (i18n) component. | yii\ |
getInstance() static | Returns the currently requested instance of this module class. | yii\ |
getLayoutPath() | Returns the directory that contains layout view files for this module. | yii\ |
getLog() | Returns the log dispatcher component. | yii\ |
getMailer() | Returns the mailer component. | yii\ |
getModule() | Retrieves the child module of the specified ID. | yii\ |
getModules() | Returns the sub-modules in this module. | yii\ |
getRequest() | Returns the request component. | Application |
getResponse() | Returns the response component. | Application |
getRuntimePath() | Returns the directory that stores runtime files. | yii\ |
getSecurity() | Returns the security component. | yii\ |
getSession() | Returns the session component. | Application |
getTimeZone() | Returns the time zone used by this application. | yii\ |
getUniqueId() | Returns an ID that uniquely identifies this module among all modules within the current application. | yii\ |
getUrlManager() | Returns the URL manager for this application. | yii\ |
getUser() | Returns the user component. | Application |
getVendorPath() | Returns the directory that stores vendor files. | yii\ |
getVersion() | Returns current module version. | yii\ |
getView() | Returns the view object. | yii\ |
getViewPath() | Returns the directory that contains the view files for this module. | yii\ |
handleRequest() | Handles the specified request. | Application |
has() | Returns a value indicating whether the locator has the specified component definition or has instantiated the component. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasModule() | Checks whether the child module of the specified ID exists. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the module. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
preInit() | Pre-initializes the application. | yii\ |
run() | Runs the application. | yii\ |
runAction() | Runs a controller action specified by a route. | yii\ |
set() | Registers a component definition with this locator. | yii\ |
setAliases() | Defines path aliases. | yii\ |
setBasePath() | Sets the root directory of the application and the @app alias. | yii\ |
setComponents() | Registers a set of component definitions in this locator. | yii\ |
setContainer() | Configures Yii::$container with the $config. | yii\ |
setControllerPath() | Sets the directory that contains the controller classes. | yii\ |
setHomeUrl() | Application | |
setInstance() static | Sets the currently requested instance of this module class. | yii\ |
setLayoutPath() | Sets the directory that contains the layout files. | yii\ |
setModule() | Adds a sub-module to this module. | yii\ |
setModules() | Registers sub-modules in the current module. | yii\ |
setRuntimePath() | Sets the directory that stores runtime files. | yii\ |
setTimeZone() | Sets the time zone used by this application. | yii\ |
setVendorPath() | Sets the directory that stores vendor files. | yii\ |
setVersion() | Sets current module version. | yii\ |
setViewPath() | Sets the directory that contains the view files. | yii\ |
trigger() | Triggers an event. | yii\ |
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. | yii\ |
defaultVersion() protected | Returns default module version. | yii\ |
registerErrorHandler() protected | Registers the errorHandler component as a PHP error handler. | yii\ |
runMappedAction() protected | Runs a standalone action registered in $actionMap. | yii\ |
runStandaloneAction() protected | Runs a standalone Action instance through the standard module/action event lifecycle. | yii\ |
Events
Section titled “Events”| Event | Type | Description | Defined by |
|---|---|---|---|
EVENT_BEFORE_REQUEST | yii\ | An event raised before the application starts to handle a request. Event name: 'beforeRequest'. | yii\ |
EVENT_AFTER_REQUEST | yii\ | An event raised after the application successfully handles a request (before the response is sent out). Event name: 'afterRequest'. | yii\ |
EVENT_BEFORE_ACTION | yii\ | An event raised before executing a controller action. Event name: 'beforeAction'. | yii\ |
EVENT_AFTER_ACTION | yii\ | An event raised after executing a controller action. Event name: 'afterAction'. | yii\ |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
STATE_BEGIN | 0 | Application state used by $state: application just started. | yii\ |
STATE_INIT | 1 | Application state used by $state: application is initializing. | yii\ |
STATE_BEFORE_REQUEST | 2 | Application state used by $state: application is triggering EVENT_BEFORE_REQUEST. | yii\ |
STATE_HANDLING_REQUEST | 3 | Application state used by $state: application is handling the request. | yii\ |
STATE_AFTER_REQUEST | 4 | Application state used by $state: application is triggering EVENT_AFTER_REQUEST. | yii\ |
STATE_SENDING_RESPONSE | 5 | Application state used by $state: application is about to send response. | yii\ |
STATE_END | 6 | Application state used by $state: application has ended. | yii\ |
Property details
Section titled “Property details”$catchAll
Section titled “$catchAll ”public array|null $catchAll = nullThe configuration specifying a controller action which should handle all user requests.
This is mainly used when the application is in maintenance mode and needs to handle all incoming requests via a single action. The configuration is an array whose first element specifies the route of the action. The rest of the array elements (key-value pairs) specify the parameters to be bound to the action. For example,
[ 'offline/notice', 'param1' => 'value1', 'param2' => 'value2',]Defaults to null, meaning catch-all is not used.
$defaultRoute
Section titled “$defaultRoute ”public string $defaultRoute = 'site'The default route of this application.
Defaults to ‘site’.
Method details
Section titled “Method details”coreComponents()
Section titled “coreComponents() ”public function coreComponents(): arrayReturns the configuration of core application components.
Returns
array
getErrorHandler()
Section titled “getErrorHandler() ”public function getErrorHandler(): yii\web\ErrorHandlerReturns the error handler component.
Returns
ErrorHandler: The error handler application component.
getHomeUrl()
Section titled “getHomeUrl() ”public function getHomeUrl(): stringReturns
string: The homepage URL
getRequest()
Section titled “getRequest() ”public function getRequest(): yii\web\RequestReturns the request component.
Returns
Request: The request component.
getResponse()
Section titled “getResponse() ”public function getResponse(): yii\web\ResponseReturns the response component.
Returns
Response: The response component.
getSession()
Section titled “getSession() ”public function getSession(): yii\web\SessionReturns the session component.
Returns
Session: The session component.
getUser()
Section titled “getUser() ”public function getUser(): yii\web\User<yii\web\TUserIdentity>Returns the user component.
Returns
User<yii\web\TUserIdentity>: The user component.
handleRequest()
Section titled “handleRequest() ”public function handleRequest(yii\web\Request $request): yii\web\ResponseHandles the specified request.
Parameters
| Parameter | Type | Description |
|---|---|---|
$request | Request | The request to be handled |
Returns
Response: The resulting response
Throws
NotFoundHttpException: if the requested route is invalid
setHomeUrl()
Section titled “setHomeUrl() ”public function setHomeUrl(string $value): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$value | string | The homepage URL |
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