CWebApplication
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).
-
widgetFactory: creates widgets and supports widget skinning.
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 and the class name must be the same as the controller ID with the first letter in upper case and appended with 'Controller'. For example, the controller 'article' is defined by the class 'ArticleController' which is in the file 'protected/controllers/ArticleController.php'.
Property Details
public CAssetManager getAssetManager()
the asset manager component
public IAuthManager getAuthManager()
the authorization manager component
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.
public CClientScript getClientScript()
Returns the client script manager.
public CController getController()
public void setController(CController $value)
the currently active controller
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.
public string $controllerNamespace;
Namespace that should be used when loading controllers. Default is to use global namespace.
public string getControllerPath()
public void setControllerPath(string $value)
the directory that contains the controller classes. Defaults to 'protected/controllers'.
public string $defaultController;
the route of the default controller, action or module. Defaults to 'site'.
public mixed $layout;
the application-wide layout. Defaults to 'main' (relative to layoutPath). If this is false, then no layout will be used.
public string getLayoutPath()
public void setLayoutPath(string $path)
the root directory of layout files. Defaults to 'protected/views/layouts'.
public CHttpSession getSession()
the session component
public string getSystemViewPath()
public void setSystemViewPath(string $path)
the root directory of system view files. Defaults to 'protected/views/system'.
public CTheme getTheme()
public void setTheme(string $value)
the theme used currently. Null if no theme is being used.
public CThemeManager getThemeManager()
the theme manager.
public CWebUser getUser()
the user session information
public string getViewPath()
public void setViewPath(string $path)
the root directory of view files. Defaults to 'protected/views'.
public IViewRenderer getViewRenderer()
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.
public IWidgetFactory getWidgetFactory()
Returns the widget factory.
Method Details
The post-filter for controller actions. This method is invoked after the currently requested controller action and all its filters are executed. You may override this method with logic that needs to be done after all controller actions.
public boolean beforeControllerAction(CController $controller, CAction $action) |
$controller | CController | the controller |
$action | CAction | the action |
{return} | boolean | whether the action should be executed. |
The pre-filter for controller actions. This method is invoked before the currently requested controller action and all its filters are executed. You may override this method with logic that needs to be done before all controller actions.
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. |
Source Code: framework/web/CWebApplication.php#310 (
show)
public function createController($route,$owner=null)
{
if($owner===null)
$owner=$this;
if((array)$route===$route || ($route=trim($route,'/'))==='')
$route=$owner->defaultController;
$caseSensitive=$this->getUrlManager()->caseSensitive;
$route.='/';
while(($pos=strpos($route,'/'))!==false)
{
$id=substr($route,0,$pos);
if(!preg_match('/^\w+$/',$id))
return null;
if(!$caseSensitive)
$id=strtolower($id);
$route=(string)substr($route,$pos+1);
if(!isset($basePath)) // first segment
{
if(isset($owner->controllerMap[$id]))
{
return array(
Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner),
$this->parseActionParams($route),
);
}
if(($module=$owner->getModule($id))!==null)
return $this->createController($route,$module);
$basePath=$owner->getControllerPath();
$controllerID='';
}
else
$controllerID.='/';
$className=ucfirst($id).'Controller';
$classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';
if($owner->controllerNamespace!==null)
$className=$owner->controllerNamespace.'\\'.str_replace('/','\\',$controllerID).$className;
if(is_file($classFile))
{
if(!class_exists($className,false))
require($classFile);
if(class_exists($className,false) && is_subclass_of($className,'CController'))
{
$id[0]=strtolower($id[0]);
return array(
new $className($controllerID.$id,$owner===$this?null:$owner),
$this->parseActionParams($route),
);
}
return null;
}
$controllerID.=$id;
$basePath.=DIRECTORY_SEPARATOR.$id;
}
}
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:
- If the first segment is found in controllerMap, the corresponding controller configuration will be used to create the controller;
- If the first segment is found to be a module ID, the corresponding module will be used to create the controller;
- 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".
public CWebModule findModule(string $id) |
$id | string | module ID |
{return} | CWebModule | the module that has the specified ID. Null if no module is found. |
Source Code: framework/web/CWebApplication.php#523 (
show)
public function findModule($id)
{
if(($controller=$this->getController())!==null && ($module=$controller->getModule())!==null)
{
do
{
if(($m=$module->getModule($id))!==null)
return $m;
} while(($module=$module->getParentModule())!==null);
}
if(($m=$this->getModule($id))!==null)
return $m;
}
Do not call this method. This method is used internally to search for a module by its ID.
Returns the client script manager.
public string getControllerPath() |
{return} | string | the directory that contains the controller classes. Defaults to 'protected/controllers'. |
Source Code: framework/web/CWebApplication.php#407 (
show)
public function getControllerPath()
{
if($this->_controllerPath!==null)
return $this->_controllerPath;
else
return $this->_controllerPath=$this->getBasePath().DIRECTORY_SEPARATOR.'controllers';
}
public string getLayoutPath() |
{return} | string | the root directory of layout files. Defaults to 'protected/views/layouts'. |
Source Code: framework/web/CWebApplication.php#473 (
show)
public function getLayoutPath()
{
if($this->_layoutPath!==null)
return $this->_layoutPath;
else
return $this->_layoutPath=$this->getViewPath().DIRECTORY_SEPARATOR.'layouts';
}
public string getSystemViewPath() |
{return} | string | the root directory of system view files. Defaults to 'protected/views/system'. |
Source Code: framework/web/CWebApplication.php#451 (
show)
public function getSystemViewPath()
{
if($this->_systemViewPath!==null)
return $this->_systemViewPath;
else
return $this->_systemViewPath=$this->getViewPath().DIRECTORY_SEPARATOR.'system';
}
public CTheme getTheme() |
{return} | CTheme | the theme used currently. Null if no theme is being used. |
public string getViewPath() |
{return} | string | the root directory of view files. Defaults to 'protected/views'. |
Source Code: framework/web/CWebApplication.php#429 (
show)
public function getViewPath()
{
if($this->_viewPath!==null)
return $this->_viewPath;
else
return $this->_viewPath=$this->getBasePath().DIRECTORY_SEPARATOR.'views';
}
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.
Returns the widget factory.
Initializes the application. This method overrides the parent implementation by preloading the 'request' component.
protected string parseActionParams(string $pathInfo) |
$pathInfo | string | path info |
{return} | string | action ID |
Source Code: framework/web/CWebApplication.php#375 (
show)
protected function parseActionParams($pathInfo)
{
if(($pos=strpos($pathInfo,'/'))!==false)
{
$manager=$this->getUrlManager();
$manager->parsePathInfo((string)substr($pathInfo,$pos+1));
$actionID=substr($pathInfo,0,$pos);
return $manager->caseSensitive ? $actionID : strtolower($actionID);
}
else
return $pathInfo;
}
Parses a path info into an action ID and GET variables.
public void processRequest() |
Source Code: framework/web/CWebApplication.php#131 (
show)
public function processRequest()
{
if(is_array($this->catchAllRequest) && isset($this->catchAllRequest[0]))
{
$route=$this->catchAllRequest[0];
foreach(array_splice($this->catchAllRequest,1) as $name=>$value)
$_GET[$name]=$value;
}
else
$route=$this->getUrlManager()->parseUrl($this->getRequest());
$this->runController($route);
}
Processes the current request. It first resolves the request into controller and action, and then creates the controller to perform the action.
protected void registerCoreComponents() |
Source Code: framework/web/CWebApplication.php#149 (
show)
protected function registerCoreComponents()
{
parent::registerCoreComponents();
$components=array(
'session'=>array(
'class'=>'CHttpSession',
),
'assetManager'=>array(
'class'=>'CAssetManager',
),
'user'=>array(
'class'=>'CWebUser',
),
'themeManager'=>array(
'class'=>'CThemeManager',
),
'authManager'=>array(
'class'=>'CPhpAuthManager',
),
'clientScript'=>array(
'class'=>'CClientScript',
),
'widgetFactory'=>array(
'class'=>'CWidgetFactory',
),
);
$this->setComponents($components);
}
Registers the core application components. This method overrides the parent implementation by registering additional core components.
public void runController(string $route) |
$route | string | the route of the current request. See createController for more details. |
Source Code: framework/web/CWebApplication.php#274 (
show)
public function runController($route)
{
if(($ca=$this->createController($route))!==null)
{
list($controller,$actionID)=$ca;
$oldController=$this->_controller;
$this->_controller=$controller;
$controller->init();
$controller->run($actionID);
$this->_controller=$oldController;
}
else
throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
array('{route}'=>$route===''?$this->defaultController:$route)));
}
Creates the controller and performs the specified action.
public void setControllerPath(string $value) |
$value | string | the directory that contains the controller classes. |
Source Code: framework/web/CWebApplication.php#419 (
show)
public function setControllerPath($value)
{
if(($this->_controllerPath=realpath($value))===false || !is_dir($this->_controllerPath))
throw new CException(Yii::t('yii','The controller path "{path}" is not a valid directory.',
array('{path}'=>$value)));
}
public void setLayoutPath(string $path) |
$path | string | the root directory of layout files. |
Source Code: framework/web/CWebApplication.php#485 (
show)
public function setLayoutPath($path)
{
if(($this->_layoutPath=realpath($path))===false || !is_dir($this->_layoutPath))
throw new CException(Yii::t('yii','The layout path "{path}" is not a valid directory.',
array('{path}'=>$path)));
}
public void setSystemViewPath(string $path) |
$path | string | the root directory of system view files. |
Source Code: framework/web/CWebApplication.php#463 (
show)
public function setSystemViewPath($path)
{
if(($this->_systemViewPath=realpath($path))===false || !is_dir($this->_systemViewPath))
throw new CException(Yii::t('yii','The system view path "{path}" is not a valid directory.',
array('{path}'=>$path)));
}
public void setTheme(string $value) |
$value | string | the theme name |
public void setViewPath(string $path) |
$path | string | the root directory of view files. |
Source Code: framework/web/CWebApplication.php#441 (
show)
public function setViewPath($path)
{
if(($this->_viewPath=realpath($path))===false || !is_dir($this->_viewPath))
throw new CException(Yii::t('yii','The view path "{path}" is not a valid directory.',
array('{path}'=>$path)));
}