Application

Type
Class
Namespace
craft\web
Inherits
craft\web\Application » yii\web\Application (opens new window) » yii\base\Application (opens new window) » yii\base\Module (opens new window) » yii\di\ServiceLocator (opens new window) » yii\base\Component (opens new window) » yii\base\BaseObject (opens new window)
Implements
yii\base\Configurable (opens new window)
Uses traits
craft\base\ApplicationTrait
Since
3.0.0

Craft Web Application class

An instance of the Web Application class is globally accessible to web requests in Craft via \Craft::$app.

View source (opens new window)

# Public Properties

Property Description
aliases (opens new window) array (opens new window) – List of path aliases to be defined.
api craft\services\Api – The API service
assetIndexer craft\services\AssetIndexer – The asset indexing service
assetManager (opens new window) yii\web\AssetManager (opens new window) – The asset manager application component.
assetTransforms craft\services\AssetTransforms – The asset transforms service
assets craft\services\Assets – The assets service
authManager (opens new window) yii\rbac\ManagerInterface (opens new window) – The auth manager application component.
basePath (opens new window) string (opens new window) – The root directory of the module.
behaviors (opens new window) yii\base\Behavior (opens new window) – List of behaviors attached to this component
bootstrap (opens new window) array (opens new window) – List of components that should be run during the application bootstrapping process (opens new window).
cache (opens new window) yii\caching\CacheInterface (opens new window) – The cache application component.
canTestEditions boolean (opens new window)
canUpgradeEdition boolean (opens new window)
catchAll (opens new window) array (opens new window) – The configuration specifying a controller action which should handle all user requests.
categories craft\services\Categories – The categories service
charset (opens new window) string (opens new window) – The charset currently used for the application.
components (opens new window) array (opens new window) – The list of the component definitions or the loaded component instances (ID => definition or instance).
composer \craft\services\Composer – The Composer service
config craft\services\Config – The config service
container (opens new window) array (opens new window) – Values given in terms of name-value pairs
content craft\services\Content – The content service
contentMigrator craft\db\MigrationManager – The content migration manager
controller (opens new window) yii\base\Controller (opens new window) – The currently active controller instance
controllerMap (opens new window) array (opens new window) – Mapping from controller ID to controller configurations.
controllerNamespace (opens new window) string (opens new window) – The namespace that controller classes are in.
controllerPath (opens new window) string (opens new window) – The directory that contains the controller classes.
dashboard craft\services\Dashboard – The dashboard service
db (opens new window) yii\db\Connection (opens new window) – The database connection.
defaultRoute (opens new window) string (opens new window) – The default route of this module.
deprecator craft\services\Deprecator – The deprecator service
drafts craft\services\Drafts – The drafts service
edition integer (opens new window)
editionName string (opens new window)
elementIndexes craft\services\ElementIndexes – The element indexes service
elements craft\services\Elements – The elements service
entries craft\services\Entries – The entries service
entryRevisions craft\services\EntryRevisions – The entry revisions service
env string (opens new window), null (opens new window) – The environment ID Craft is currently running in.
errorHandler (opens new window) yii\web\ErrorHandler (opens new window), yii\console\ErrorHandler (opens new window) – The error handler application component.
extensions (opens new window) array (opens new window) – List of installed Yii extensions.
feeds craft\feeds\Feeds – The feeds service
fields craft\services\Fields – The fields service
formatter (opens new window) yii\i18n\Formatter (opens new window) – The formatter application component.
gc craft\services\Gc – The garbage collection service
globals craft\services\Globals – The globals service
gql craft\services\Gql – The GraphQL service
hasWrongEdition boolean (opens new window)
homeUrl (opens new window) string (opens new window) – The homepage URL
i18n (opens new window) yii\i18n\I18N (opens new window) – The internationalization application component.
id (opens new window) string (opens new window) – An ID that uniquely identifies this module among other modules which have the same parent (opens new window).
images craft\services\Images – The images service
info craft\models\Info
installedSchemaVersion string (opens new window)
isDbConnectionValid boolean (opens new window)
isInMaintenanceMode boolean (opens new window)
isInitialized boolean (opens new window)
isInstalled boolean (opens new window)
isLive boolean (opens new window)
isMultiSite boolean (opens new window)
isSystemOn boolean (opens new window)
language (opens new window) string (opens new window) – The language that is meant to be used for end users.
layout (opens new window) string (opens new window), boolean (opens new window) – The layout that should be applied for views within this module.
layoutPath (opens new window) string (opens new window) – The root directory of layout files.
licensedEdition integer (opens new window), null (opens new window)
licensedEditionName string (opens new window), null (opens new window)
loadedModules (opens new window) array (opens new window) – List of loaded modules indexed by their class names.
locale craft\i18n\Locale – The Locale object for the target language
log (opens new window) yii\log\Dispatcher (opens new window) – The log dispatcher application component.
mailer craft\mail\Mailer – The mailer component
matrix craft\services\Matrix – The matrix service
migrator craft\db\MigrationManager – The application’s migration manager
minVersionRequired string (opens new window), null (opens new window) – The minimum Craft build number required to update to this build.
module (opens new window) yii\base\Module (opens new window) – The parent module of this module.
modules (opens new window) array (opens new window) – The modules (indexed by their IDs).
mutex yii\mutex\Mutex (opens new window) – The application’s mutex service
name (opens new window) string (opens new window) – The application name.
params (opens new window) array (opens new window) – Custom module parameters (name => value).
path craft\services\Path – The path service
pluginStore craft\services\PluginStore – The plugin store service
plugins craft\services\Plugins – The plugins service
projectConfig craft\services\ProjectConfig – The system config service
queue craft\queue\Queue, craft\queue\QueueInterface – The queue service
relations craft\services\Relations – The relations service
request (opens new window) yii\web\Request (opens new window), yii\console\Request (opens new window) – The request component.
requestedAction (opens new window) yii\base\Action (opens new window) – The requested Action.
requestedParams (opens new window) array (opens new window) – The parameters supplied to the requested action.
requestedRoute (opens new window) string (opens new window) – The requested route
response (opens new window) yii\web\Response (opens new window), yii\console\Response (opens new window) – The response component.
revisions craft\services\Revisions – The revisions service
routes craft\services\Routes – The routes service
runtimePath (opens new window) string (opens new window) – The directory that stores runtime files.
schemaVersion string (opens new window), null (opens new window) – Craft’s schema version number.
search craft\services\Search – The search service
sections craft\services\Sections – The sections service
security (opens new window) yii\base\Security (opens new window) – The security application component.
session (opens new window) yii\web\Session (opens new window) – The session component.
sites craft\services\Sites – The sites service
sourceLanguage (opens new window) string (opens new window) – The language that the application is written in.
state (opens new window) integer (opens new window) – The current application state during a request handling life cycle.
structures craft\services\Structures – The structures service
systemMessages craft\services\SystemMessages – The system email messages service
systemName string (opens new window)
systemSettings craft\services\SystemSettings – The system settings service
systemUid string (opens new window), null (opens new window)
tags craft\services\Tags – The tags service
targetLanguage string (opens new window)
templateCaches craft\services\TemplateCaches – The template caches service
timeZone (opens new window) string (opens new window) – The time zone used by this application.
tokens craft\services\Tokens – The tokens service
uniqueId (opens new window) string (opens new window) – The unique ID of the module.
updates craft\services\Updates – The updates service
urlManager (opens new window) yii\web\UrlManager (opens new window) – The URL manager for this application.
user (opens new window) yii\web\User (opens new window) – The user component.
userGroups craft\services\UserGroups – The user groups service
userPermissions craft\services\UserPermissions – The user permissions service
users craft\services\Users – The users service
utilities craft\services\Utilities – The utilities service
vendorPath (opens new window) string (opens new window) – The directory that stores vendor files.
version (opens new window) string (opens new window) – The version of this module.
view (opens new window) yii\base\View (opens new window), yii\web\View (opens new window) – The view application component that is used to render various view files.
viewPath (opens new window) string (opens new window) – The root directory of view files.
volumes craft\services\Volumes – The volumes service
yiiVersion string (opens new window)

# Public Methods

Method Description
__call() (opens new window) Calls the named method which is not a class method.
__clone() (opens new window) This method is called after the object is created by cloning an existing one.
__construct() (opens new window) Constructor.
__get() (opens new window) Returns the value of an object property.
__isset() (opens new window) Checks if a property is set, i.e. defined and not null.
__set() (opens new window) Sets value of an object property.
__unset() (opens new window) Sets an object property to null.
afterAction() (opens new window) This method is invoked right after an action within this module is executed.
attachBehavior() (opens new window) Attaches a behavior to this component.
attachBehaviors() (opens new window) Attaches a list of behaviors to the component.
beforeAction() (opens new window) This method is invoked right before an action within this module is executed.
behaviors() (opens new window) Returns a list of behaviors that this component should behave as.
bootstrap() Initializes extensions and executes bootstrap components.
canGetProperty() (opens new window) Returns a value indicating whether a property can be read.
canSetProperty() (opens new window) Returns a value indicating whether a property can be set.
className() (opens new window) Returns the fully qualified name of this class.
clear() (opens new window) Removes the component from the locator.
coreComponents() (opens new window) Returns the configuration of core application components.
createController() (opens new window) Creates a controller instance based on the given route.
createControllerByID() (opens new window) Creates a controller based on the given controller ID.
detachBehavior() (opens new window) Detaches a behavior from the component.
detachBehaviors() (opens new window) Detaches all behaviors from the component.
disableMaintenanceMode() Disables Maintenance Mode.
enableMaintenanceMode() Enables Maintenance Mode.
end() (opens new window) Terminates the application.
ensureBehaviors() (opens new window) Makes sure that the behaviors declared in behaviors() (opens new window) are attached to this component.
get() Returns the component instance with the specified ID.
getApi() Returns the API service.
getAssetIndexer() Returns the asset indexing service.
getAssetManager() (opens new window) Returns the asset manager.
getAssetTransforms() Returns the asset transforms service.
getAssets() Returns the assets service.
getAuthManager() (opens new window) Returns the auth manager for this application.
getBasePath() (opens new window) Returns the root directory of the module.
getBehavior() (opens new window) Returns the named behavior object.
getBehaviors() (opens new window) Returns all behaviors attached to this component.
getCache() (opens new window) Returns the cache component.
getCanTestEditions() Returns whether Craft is running on a domain that is eligible to test out the editions.
getCanUpgradeEdition() Returns whether Craft is eligible to be upgraded to a different edition.
getCategories() Returns the categories service.
getComponents() (opens new window) Returns the list of the component definitions or the loaded component instances.
getComposer() Returns the Composer service.
getConfig() Returns the config service.
getContent() Returns the content service.
getContentMigrator() Returns the content migration manager.
getControllerPath() (opens new window) Returns the directory that contains the controller classes according to controllerNamespace (opens new window).
getDashboard() Returns the dashboard service.
getDb() (opens new window) Returns the database connection component.
getDeprecator() Returns the deprecator service.
getDrafts() Returns the drafts service.
getEdition() Returns the Craft edition.
getEditionName() Returns the name of the Craft edition.
getElementIndexes() Returns the element indexes service.
getElements() Returns the elements service.
getEntries() Returns the entries service.
getEntryRevisions() Returns the entry revisions service.
getErrorHandler() (opens new window) Returns the error handler component.
getFeeds() Returns the feeds service.
getFields() Returns the fields service.
getFormatter() (opens new window) Returns the formatter component.
getGc() Returns the garbage collection service.
getGlobals() Returns the globals service.
getGql() Returns the GraphQL service.
getHasWrongEdition() Returns whether Craft is running with the wrong edition.
getHomeUrl() (opens new window)
getI18n() (opens new window) Returns the internationalization (i18n) component.
getImages() Returns the images service.
getInfo() Returns the info model, or just a particular attribute.
getInstalledSchemaVersion() Returns the installed schema version.
getInstance() (opens new window) Returns the currently requested instance of this module class.
getIsInMaintenanceMode() Returns whether someone is currently performing a system update.
getIsInitialized() Returns whether Craft has been fully initialized.
getIsInstalled() Returns whether Craft is installed.
getIsLive() Returns whether the system is currently live.
getIsMultiSite() Returns whether this Craft install has multiple sites.
getIsSystemOn() Returns whether the system is currently live.
getLayoutPath() (opens new window) Returns the directory that contains layout view files for this module.
getLicensedEdition() Returns the edition Craft is actually licensed to run in.
getLicensedEditionName() Returns the name of the edition Craft is actually licensed to run in.
getLocale() Returns a Locale object for the target language.
getLog() (opens new window) Returns the log dispatcher component.
getMailer() Returns the current mailer.
getMatrix() Returns the matrix service.
getMigrator() Returns the application’s migration manager.
getModule() (opens new window) Retrieves the child module of the specified ID.
getModules() (opens new window) Returns the sub-modules in this module.
getMutex() Returns the application’s mutex service.
getPath() Returns the path service.
getPluginStore() Returns the plugin store service.
getPlugins() Returns the plugins service.
getProjectConfig() Returns the system config service.
getQueue() Returns the queue service.
getRelations() Returns the relations service.
getRequest() (opens new window) Returns the request component.
getResponse() (opens new window) Returns the response component.
getRevisions() Returns the revisions service.
getRoutes() Returns the routes service.
getRuntimePath() (opens new window) Returns the directory that stores runtime files.
getSearch() Returns the search service.
getSections() Returns the sections service.
getSecurity() (opens new window) Returns the security component.
getSession() (opens new window) Returns the session component.
getSites() Returns the sites service.
getStructures() Returns the structures service.
getSystemMessages() Returns the system email messages service.
getSystemName() Returns the system name.
getSystemSettings() Returns the system settings service.
getSystemUid() Returns the system's UID.
getTags() Returns the tags service.
getTargetLanguage() Returns the target app language.
getTemplateCaches() Returns the template cache service.
getTimeZone() (opens new window) Returns the time zone used by this application.
getTokens() Returns the tokens service.
getUniqueId() (opens new window) Returns an ID that uniquely identifies this module among all modules within the current application.
getUpdates() Returns the updates service.
getUrlManager() (opens new window) Returns the URL manager for this application.
getUser() (opens new window) Returns the user component.
getUserGroups() Returns the user groups service.
getUserPermissions() Returns the user permissions service.
getUsers() Returns the users service.
getUtilities() Returns the utilities service.
getVendorPath() (opens new window) Returns the directory that stores vendor files.
getVersion() (opens new window) Returns current module version.
getView() (opens new window) Returns the view object.
getViewPath() (opens new window) Returns the directory that contains the view files for this module.
getVolumes() Returns the volumes service.
getYiiVersion() Returns the Yii framework version.
handleRequest() Handles the specified request.
has() (opens new window) Returns a value indicating whether the locator has the specified component definition or has instantiated the component.
hasEventHandlers() (opens new window) Returns a value indicating whether there is any handler attached to the named event.
hasMethod() (opens new window) Returns a value indicating whether a method is defined.
hasModule() (opens new window) Checks whether the child module of the specified ID exists.
hasProperty() (opens new window) Returns a value indicating whether a property is defined.
init() Initializes the application.
off() (opens new window) Detaches an existing event handler from this component.
on() (opens new window) Attaches an event handler to an event.
preInit() (opens new window) Pre-initializes the application.
requireEdition() Requires that Craft is running an equal or better edition than what's passed in
run() (opens new window) Runs the application.
runAction() Runs a controller action specified by a route.
saveInfo() Updates the info row.
saveInfoAfterRequest() Updates the info row at the end of the request.
set() (opens new window) Registers a component definition with this locator.
setAliases() (opens new window) Defines path aliases.
setBasePath() (opens new window) Sets the root directory of the module.
setComponents() (opens new window) Registers a set of component definitions in this locator.
setContainer() (opens new window) Configures Yii::$container with the $config.
setEdition() Sets the Craft edition.
setHomeUrl() (opens new window)
setInstance() (opens new window) Sets the currently requested instance of this module class.
setIsInstalled() Sets Craft's record of whether it's installed
setLayoutPath() (opens new window) Sets the directory that contains the layout files.
setModule() (opens new window) Adds a sub-module to this module.
setModules() (opens new window) Registers sub-modules in the current module.
setRuntimePath() (opens new window) Sets the directory that stores runtime files.
setTimeZone() Sets the time zone used by this application.
setVendorPath() Sets the directory that stores vendor files.
setVersion() (opens new window) Sets current module version.
setViewPath() (opens new window) Sets the directory that contains the view files.
trigger() (opens new window) Triggers an event.
updateTargetLanguage() Sets the target application language.

# bootstrap()

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.

View source (opens new window)

# get()

Returns the component instance with the specified ID.

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.

View source (opens new window)

Arguments

Returns

object (opens new window), null (opens new window) – The component of the specified ID. If $throwException is false and $id is not registered before, null will be returned.

Throws

# handleRequest()

Handles the specified request.

View source (opens new window)

Arguments

Returns

yii\web\Response (opens new window) – The resulting response

Throws

# init()

Initializes the application.

View source (opens new window)

# runAction()

Runs a controller action specified by a route.

This method parses the specified route and creates the corresponding child module(s), controller and action instances. It then calls craft\web\Controller::runAction() to run the action with the given parameters. If the route is empty, the method will use defaultRoute (opens new window).

View source (opens new window)

Arguments

Returns

yii\web\Response (opens new window), null (opens new window) – The result of the action, normalized into a Response object

Throws

# setTimeZone()

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 (opens new window) for available timezones.

View source (opens new window)

Arguments

# setVendorPath()

Sets the directory that stores vendor files.

View source (opens new window)

Arguments

# Protected Methods

Method Description
authenticate() Authenticates the request.
debugBootstrap() Bootstraps the Debug Toolbar if necessary.
defaultVersion() (opens new window) Returns default module version.
ensureResourcePathExists() Ensures that the resources folder exists and is writable.
registerErrorHandler() (opens new window) Registers the errorHandler component as a PHP error handler.

# authenticate()

Since
3.5.0

Authenticates the request.

View source (opens new window)

Throws

# debugBootstrap()

Bootstraps the Debug Toolbar if necessary.

View source (opens new window)

# ensureResourcePathExists()

Ensures that the resources folder exists and is writable.

View source (opens new window)

Throws

# Constants

Constant Description
STATE_AFTER_REQUEST Application state used by state (opens new window): application is triggering EVENT_AFTER_REQUEST (opens new window).
STATE_BEFORE_REQUEST Application state used by state (opens new window): application is triggering EVENT_BEFORE_REQUEST (opens new window).
STATE_BEGIN Application state used by state (opens new window): application just started.
STATE_END Application state used by state (opens new window): application has ended.
STATE_HANDLING_REQUEST Application state used by state (opens new window): application is handling the request.
STATE_INIT Application state used by state (opens new window): application is initializing.
STATE_SENDING_RESPONSE Application state used by state (opens new window): application is about to send response.

# Events

# EVENT_AFTER_EDITION_CHANGE

Type
craft\events\EditionChangeEvent

The event that is triggered after the edition changes

# EVENT_INIT

Type
yii\base\Event (opens new window)

The event that is triggered after the application has been fully initialized

Example

use craft\web\Application;

Craft::$app->on(Application::EVENT_INIT, function() {
    // ...
});