CWebApplication

Package system.web
Inheritance class CWebApplication » CApplication » CComponent
Since 1.0
Version $Id: CWebApplication.php 771 2009-02-28 14:18:20Z qiang.xue $
CWebApplication extends CApplication by providing functionalities specific to Web requests.

CWebApplication manages the controllers in MVC pattern, and provides the following additional core application components:
  • urlManager: provides URL parsing and constructing functionality;
  • request: encapsulates the Web request information;
  • session: provides the session-related functionalities;
  • assetManager: manages the publishing of private asset files.
  • user: represents the user session information.
  • themeManager: manages themes.
  • authManager: manages role-based access control (RBAC).
  • clientScript: manages client scripts (javascripts and CSS).


User requests are resolved as controller-action pairs and additional parameters. CWebApplication creates the requested controller instance and let it to handle the actual user request. If the user does not specify controller ID, it will assume defaultController is requested (which defaults to 'site').

Controller class files must reside under the directory controllerPath (defaults to 'protected/controllers'). The file name is the same as the controller name and the class name is the controller ID appended with 'Controller'. For example, the controller 'article' is defined by the class 'ArticleController' which is in the file 'protected/controller/article.php'.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
assetManager CAssetManager the asset manager component CWebApplication
authManager IAuthManager the authorization manager component CWebApplication
basePath string the root directory of the application. CApplication
baseUrl string Returns the relative URL for the application. CWebApplication
behaviors array the behaviors that should be attached to the application. CApplication
cache CCache the cache application component. CApplication
catchAllRequest array the configuration specifying a controller which should handle all user requests. CWebApplication
charset string the charset currently used for the application. CApplication
clientScript CClientScript Returns the client script manager. CWebApplication
components array the currently loaded components (indexed by their IDs) CApplication
controller CController the currently active controller CWebApplication
controllerMap array mapping from controller ID to controller configurations. CWebApplication
controllerPath string the directory that contains the controller classes. CWebApplication
coreMessages CPhpMessageSource the core message translations CApplication
dateFormatter CDateFormatter the locale-dependent date formatter. CApplication
db CDbConnection the database connection CApplication
defaultController string the ID of the default controller. CWebApplication
errorHandler CErrorHandler the error handler application component. CApplication
extensionPath string Returns the root directory that holds all third-party extensions. CApplication
homeUrl string the homepage URL CWebApplication
id string a unique identifier for the application. CApplication
language string the language that the user is using and the application should be targeted to. CApplication
layout mixed the application-wide layout. CWebApplication
layoutPath string the root directory of layout files. CWebApplication
locale CLocale the locale instance CApplication
messages CMessageSource the application message translations CApplication
modulePath string the directory that contains the application modules. CWebApplication
modules array the configurations of the currently installed modules (id=>configuration) CWebApplication
name string the application name. CApplication
numberFormatter CNumberFormatter the locale-dependent number formatter. CApplication
params CAttributeCollection the list of application parameters CApplication
preload array the IDs of the application components that should be preloaded. CApplication
request CHttpRequest the request component CWebApplication
runtimePath string the directory that stores runtime files. CApplication
securityManager CSecurityManager the security manager application component. CApplication
session CHttpSession the session component CWebApplication
sourceLanguage string the language that the application is written in. CApplication
statePersister CStatePersister the state persister application component. CApplication
systemViewPath string the root directory of system view files. CWebApplication
theme CTheme the theme used currently. CWebApplication
themeManager CThemeManager the theme manager. CWebApplication
urlManager CUrlManager the URL manager component CWebApplication
user CWebUser the user session information CWebApplication
viewPath string the root directory of view files. CWebApplication
viewRenderer IViewRenderer Returns the view renderer. CWebApplication

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CApplication
__get() Getter magic method. CApplication
__isset() Checks if a property value is null. CApplication
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
clearGlobalState() Clears a global value. CApplication
configure() Configures the application with the specified configuration. CApplication
createAbsoluteUrl() Creates an absolute URL based on the given controller and action information. CWebApplication
createController() Creates a controller instance based on a route. CWebApplication
createModule() Creates an application module based on the given configuration. CWebApplication
createUrl() Creates a relative URL based on the given controller and action information. CWebApplication
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
displayError() Displays the captured PHP error. CApplication
displayException() Displays the uncaught PHP exception. CApplication
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
end() Terminates the application. CApplication
findLocalizedFile() Returns the localized version of a specified file. CApplication
findModule() Searches for a module by its ID. CWebApplication
getAssetManager() CWebApplication
getAuthManager() CWebApplication
getBasePath() CApplication
getBaseUrl() Returns the relative URL for the application. CWebApplication
getCache() CApplication
getClientScript() Returns the client script manager. CWebApplication
getComponent() Retrieves the named application component. CApplication
getComponents() CApplication
getController() CWebApplication
getControllerPath() CWebApplication
getCoreMessages() CApplication
getDateFormatter() CApplication
getDb() CApplication
getErrorHandler() CApplication
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getExtensionPath() Returns the root directory that holds all third-party extensions. CApplication
getGlobalState() Returns a global value. CApplication
getHomeUrl() CWebApplication
getId() CApplication
getLanguage() CApplication
getLayoutPath() CWebApplication
getLocale() CApplication
getMessages() CApplication
getModule() Retrieves the named application module. CWebApplication
getModulePath() CWebApplication
getModules() CWebApplication
getNumberFormatter() CApplication
getParams() CApplication
getRequest() CWebApplication
getRuntimePath() CApplication
getSecurityManager() CApplication
getSession() CWebApplication
getStatePersister() CApplication
getSystemViewPath() CWebApplication
getTheme() CWebApplication
getThemeManager() CWebApplication
getUrlManager() CWebApplication
getUser() CWebApplication
getViewPath() CWebApplication
getViewRenderer() Returns the view renderer. CWebApplication
handleError() Handles PHP execution errors such as warnings, notices. CApplication
handleException() Handles uncaught PHP exceptions. CApplication
hasComponent() CApplication
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
onBeginRequest() Raised right BEFORE the application processes the request. CApplication
onEndRequest() Raised right AFTER the application processes the request. CApplication
onError() Raised when a PHP execution error occurs. CApplication
onException() Raised when an uncaught PHP exception occurs. CApplication
processRequest() Processes the current request. CWebApplication
raiseEvent() Raises an event. CComponent
run() Runs the application. CApplication
runController() Creates the controller and performs the specified action. CWebApplication
setBasePath() Sets the root directory of the application. CApplication
setComponent() Puts a component under the management of the application. CApplication
setComponents() Sets the application components. CApplication
setControllerPath() CWebApplication
setGlobalState() Sets a global value. CApplication
setHomeUrl() CWebApplication
setId() CApplication
setImport() Sets the aliases that are used in the application. CApplication
setLanguage() Specifies which language the application is targeted to. CApplication
setLayoutPath() CWebApplication
setModulePath() CWebApplication
setModules() Configures the modules of this application. CWebApplication
setParams() CApplication
setRuntimePath() CApplication
setSystemViewPath() CWebApplication
setTheme() CWebApplication
setViewPath() CWebApplication

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
init() Initializes the application. CApplication
initSystemHandlers() Initializes the class autoloader and error handlers. CApplication
loadGlobalState() Loads the global state data from persistent storage. CApplication
parseActionParams() Parses a path info into an action ID and GET variables. CWebApplication
preloadComponents() Loads static application components. CApplication
registerCoreComponents() Registers the core application components. CWebApplication
saveGlobalState() Saves the global state data into persistent storage. CApplication

Events

Hide inherited events

EventDescriptionDefined By
onBeginRequest Raised right BEFORE the application processes the request. CApplication
onEndRequest Raised right AFTER the application processes the request. CApplication
onException Raised when an uncaught PHP exception occurs. CApplication
onError Raised when a PHP execution error occurs. CApplication

Property Details

assetManager property read-only

the asset manager component

authManager property read-only

the authorization manager component

baseUrl property read-only
public string getBaseUrl(boolean $absolute=false)

Returns the relative URL for the application. This is a shortcut method to CHttpRequest::getBaseUrl().

catchAllRequest property
public array $catchAllRequest;

the configuration specifying a controller which should handle all user requests. This is mainly used when the application is in maintenance mode and we should use a controller to handle all incoming requests. The configuration specifies the controller route (the first element) and GET parameters (the rest name-value pairs). For example,

array(
    'offline/notice',
    'param1'=>'value1',
    'param2'=>'value2',
)
Defaults to null, meaning catch-all is not effective.

clientScript property read-only

Returns the client script manager.

controller property read-only

the currently active controller

controllerMap property
public array $controllerMap;

mapping from controller ID to controller configurations. Each name-value pair specifies the configuration for a single controller. A controller configuration can be either a string or an array. If the former, the string should be the class name or class path alias of the controller. If the latter, the array must contain a 'class' element which specifies the controller's class name or class path alias. The rest name-value pairs in the array are used to initialize the corresponding controller properties. For example,

array(
  'post'=>array(
     'class'=>'path.to.PostController',
     'pageTitle'=>'something new',
  ),
  'user'=>'path.to.UserController',,
)


Note, when processing an incoming request, the controller map will first be checked to see if the request can be handled by one of the controllers in the map. If not, a controller will be searched for under the default controller path.

controllerPath property
public string getControllerPath()
public void setControllerPath(string $value)

the directory that contains the controller classes. Defaults to 'protected/controllers'.

defaultController property
public string $defaultController;

the ID of the default controller. Defaults to 'site'.

homeUrl property
public string getHomeUrl()
public void setHomeUrl(string $value)

the homepage URL

layout property
public mixed $layout;

the application-wide layout. Defaults to 'main' (relative to layoutPath). If this is false, then no layout will be used.

layoutPath property
public string getLayoutPath()
public void setLayoutPath(string $path)

the root directory of layout files. Defaults to 'protected/views/layouts'.

modulePath property (available since v1.0.3)
public string getModulePath()
public void setModulePath(string $value)

the directory that contains the application modules. Defaults to 'protected/modules'.

modules property (available since v1.0.3)
public array getModules()
public void setModules(array $modules)

the configurations of the currently installed modules (id=>configuration)

request property read-only

the request component

session property read-only

the session component

systemViewPath property
public string getSystemViewPath()
public void setSystemViewPath(string $path)

the root directory of system view files. Defaults to 'protected/views/system'.

theme property
public CTheme getTheme()
public void setTheme(string $value)

the theme used currently. Null if no theme is being used.

themeManager property read-only

the theme manager.

urlManager property read-only

the URL manager component

user property read-only
public CWebUser getUser()

the user session information

viewPath property
public string getViewPath()
public void setViewPath(string $path)

the root directory of view files. Defaults to 'protected/views'.

viewRenderer property read-only

Returns the view renderer. If this component is registered and enabled, the default view rendering logic defined in CBaseController will be replaced by this renderer.

Method Details

createAbsoluteUrl() method
public string createAbsoluteUrl(string $route, array $params=array ( ), string $schema='', string $ampersand='&')
$route string the URL route. This should be in the format of 'ControllerID/ActionID'.
$params array additional GET parameters (name=>value). Both the name and value will be URL-encoded.
$schema string schema to use (e.g. http, https). If empty, the schema used for the current request will be used.
$ampersand string the token separating name-value pairs in the URL.
{return} string the constructed URL

Creates an absolute URL based on the given controller and action information.

createController() method
public array createController(string $route, CWebModule $owner=NULL)
$route string the route of the request.
$owner CWebModule the module that the new controller will belong to. Defaults to null, meaning the application instance is the owner.
{return} array the controller instance and the action ID. Null if the controller class does not exist or the route is invalid.

Creates a controller instance based on a route. The route should contain the controller ID and the action ID. It may also contain additional GET variables. All these must be concatenated together with slashes.

This method will attempt to create a controller in the following order:

  1. If the first segment is found in controllerMap, the corresponding controller configuration will be used to create the controller;
  2. If the first segment is found to be a module ID, the corresponding module will be used to create the controller;
  3. Otherwise, it will search under the controllerPath to create the corresponding controller. For example, if the route is "admin/user/create", then the controller will be created using the class file "protected/controllers/admin/UserController.php".

createModule() method (available since v1.0.3)
public CWebModule createModule(string $id, array $config, CWebModule $owner=NULL)
$id string the module ID
$config array module configuration
$owner CWebModule the parent module. Null if no parent.
{return} CWebModule the create module instance. Null if the module is not enabled.

Creates an application module based on the given configuration. The module created will be initialized with the given configuration. And its CWebModule::init() method will also be invoked.

createUrl() method
public string createUrl(string $route, array $params=array ( ), string $ampersand='&')
$route string the URL route. This should be in the format of 'ControllerID/ActionID'.
$params array additional GET parameters (name=>value). Both the name and value will be URL-encoded.
$ampersand string the token separating name-value pairs in the URL.
{return} string the constructed URL

Creates a relative URL based on the given controller and action information.

findModule() method (available since v1.0.3)
public CWebModule findModule(string $id)
$id string module ID
{return} CWebModule the module that has the specified ID. Null if no module is found.

Searches for a module by its ID. This method is used internally. Do not call this method.

getAssetManager() method
public CAssetManager getAssetManager()
{return} CAssetManager the asset manager component

getAuthManager() method
public IAuthManager getAuthManager()
{return} IAuthManager the authorization manager component

getBaseUrl() method
public string getBaseUrl(boolean $absolute=false)
$absolute boolean whether to return an absolute URL. Defaults to false, meaning returning a relative one. This parameter has been available since 1.0.2.
{return} string the relative URL for the application

Returns the relative URL for the application. This is a shortcut method to CHttpRequest::getBaseUrl().

getClientScript() method
public CClientScript getClientScript()
{return} CClientScript the client script manager

Returns the client script manager.

getController() method
public CController getController()
{return} CController the currently active controller

getControllerPath() method
public string getControllerPath()
{return} string the directory that contains the controller classes. Defaults to 'protected/controllers'.

getHomeUrl() method
public string getHomeUrl()
{return} string the homepage URL

getLayoutPath() method
public string getLayoutPath()
{return} string the root directory of layout files. Defaults to 'protected/views/layouts'.

getModule() method (available since v1.0.3)
public CWebModule getModule(string $id)
$id string application module ID (case-sensitive)
{return} CWebModule the application module instance, null if the application module is disabled or does not exist.

Retrieves the named application module.

getModulePath() method (available since v1.0.3)
public string getModulePath()
{return} string the directory that contains the application modules. Defaults to 'protected/modules'.

getModules() method (available since v1.0.3)
public array getModules()
{return} array the configurations of the currently installed modules (id=>configuration)

getRequest() method
public CHttpRequest getRequest()
{return} CHttpRequest the request component

getSession() method
public CHttpSession getSession()
{return} CHttpSession the session component

getSystemViewPath() method
public string getSystemViewPath()
{return} string the root directory of system view files. Defaults to 'protected/views/system'.

getTheme() method
public CTheme getTheme()
{return} CTheme the theme used currently. Null if no theme is being used.

getThemeManager() method
public CThemeManager getThemeManager()
{return} CThemeManager the theme manager.

getUrlManager() method
public CUrlManager getUrlManager()
{return} CUrlManager the URL manager component

getUser() method
public CWebUser getUser()
{return} CWebUser the user session information

getViewPath() method
public string getViewPath()
{return} string the root directory of view files. Defaults to 'protected/views'.

getViewRenderer() method
public IViewRenderer getViewRenderer()
{return} IViewRenderer the view renderer.

Returns the view renderer. If this component is registered and enabled, the default view rendering logic defined in CBaseController will be replaced by this renderer.

parseActionParams() method (available since v1.0.3)
protected string parseActionParams(string $pathInfo)
$pathInfo string path info
{return} string action ID

Parses a path info into an action ID and GET variables.

processRequest() method
public void processRequest()

Processes the current request. It first resolves the request into controller and action, and then creates the controller to perform the action.

registerCoreComponents() method
protected void registerCoreComponents()

Registers the core application components. This method overrides the parent implementation by registering additional core components.

See Also

runController() method
public void runController(string $route)
$route string the route of the current request. See createController for more details.

Creates the controller and performs the specified action.

setControllerPath() method
public void setControllerPath(string $value)
$value string the directory that contains the controller classes.

setHomeUrl() method
public void setHomeUrl(string $value)
$value string the homepage URL

setLayoutPath() method
public void setLayoutPath(string $path)
$path string the root directory of layout files.

setModulePath() method (available since v1.0.3)
public void setModulePath(string $value)
$value string the directory that contains the application modules.

setModules() method (available since v1.0.3)
public void setModules(array $modules)
$modules array application module configuration.

Configures the modules of this application.

Call this method to declare modules and configure them with their initial property values. The parameter should be an array of module configurations. Each array element represents a single module, which can be either a string representing the module ID or an ID-config pair representing a module with the specified ID and the initial property values.

For example, the following array declares two modules:

array(
    'admin',
    'payment'=>array(
        'server'=>'paymentserver.com',
    ),
)


By default, the module class is determined using the expression ucfirst($moduleID).'Module'. And the class file is located under modules/$moduleID. You may override this default by explicitly specifying the 'class' option in the configuration.

You may also enable or disable a module by specifying the 'enabled' option in the configuration.

setSystemViewPath() method
public void setSystemViewPath(string $path)
$path string the root directory of system view files.

setTheme() method
public void setTheme(string $value)
$value string the theme name

setViewPath() method
public void setViewPath(string $path)
$path string the root directory of view files.