* @link http://www.yiiframework.com/ * @copyright Copyright © 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ /** * CController manages a set of actions which deal with the corresponding user requests. * * Through the actions, CController coordinates the data flow between models and views. * * When a user requests an action 'XYZ', CController will do one of the following: * 1. Method-based action: call method 'actionXYZ' if it exists; * 2. Class-based action: create an instance of class 'XYZ' if the class is found in the action class map * (specified via {@link actions()}, and execute the action; * 3. Call {@link missingAction()}, which by default will raise a 404 HTTP exception. * * If the user does not specify an action, CController will run the action specified by * {@link defaultAction}, instead. * * CController may be configured to execute filters before and after running actions. * Filters preprocess/postprocess the user request/response and may quit executing actions * if needed. They are executed in the order they are specified. If during the execution, * any of the filters returns true, the rest filters and the action will no longer get executed. * * Filters can be individual objects, or methods defined in the controller class. * They are specified by overriding {@link filters()} method. The following is an example * of the filter specification: *
 * array(
 *     'accessControl - login',
 *     'ajaxOnly + search',
 *     array(
 *         'COutputCache + list',
 *         'duration'=>300,
 *     ),
 * )
 * 
* The above example declares three filters: accessControl, ajaxOnly, COutputCache. The first two * are method-based filters (defined in CController), which refer to filtering methods in the controller class; * while the last refers to a object-based filter whose class is 'system.web.widgets.COutputCache' and * the 'duration' property is initialized as 300 (s). * * For method-based filters, a method named 'filterXYZ($filterChain)' in the controller class * will be executed, where 'XYZ' stands for the filter name as specified in {@link filters()}. * Note, inside the filter method, you must call $filterChain->run() if the action should * be executed. Otherwise, the filtering process would stop at this filter. * * Filters can be specified so that they are executed only when running certain actions. * For method-based filters, this is done by using '+' and '-' operators in the filter specification. * The '+' operator means the filter runs only when the specified actions are requested; * while the '-' operator means the filter runs only when the requested action is not among those actions. * For object-based filters, the '+' and '-' operators are following the class name. * * @author Qiang Xue * @version $Id$ * @package system.web * @since 1.0 */ class CController extends CBaseController { /** * @var mixed the name of the layout to be applied to this controller's views. * Defaults to null, meaning the {@link CWebApplication::layout application layout} * is used. If false, no layout will be applied. */ public $layout; /** * @var string the name of the default action. Defaults to 'index'. */ public $defaultAction='index'; private $_id; private $_action; private $_pageTitle; private $_clientScript; private $_cachingStack; private $_clips; private $_dynamicOutput; /** * @param string id of this controller */ public function __construct($id) { $this->_id=$id; } /** * Returns the filter configurations. * * By overriding this method, child classes can specify filters to be applied to actions. * * This method returns an array of filter specifications. Each array element specify a single filter. * * For a method-based filter (called inline filter), it is specified as 'FilterName[ +|- Action1, Action2, ...]', * where the '+' ('-') operators describe which actions should be (should not be) applied with the filter. * * For a class-based filter, it is specified as an array like the following: *
	 * array('class'=>'Path.To.FilterClass', 'name1'=>'value1', 'name2'=>'value2'...)
	 * 
* where the name-value pairs will be used to initialize the properties of the filter. * * @return array a list of filter configurations. * @see CFilter */ public function filters() { return array(); } /** * Returns a list of external action classes. * Array keys are action names, and array values are the corresponding * action class in dot syntax (e.g. 'edit'=>'application.controllers.article.EditArticle') * or arrays representing the configuration of the actions, such as the following, *
	 * array(
	 *     'class'=>'CViewAction',
	 *     'views'=>'about,help',
	 * )
	 * 
* Derived classes may override this method to make use of external action feature. * @return array list of external action classes * @see createAction */ public function actions() { return array(); } /** * Returns the access rules for this controller. * Override this method if you use the {@link filterAccessControl accessControl} filter. * @return array list of access rules. See {@link CAccessControlFilter} for details about rule specification. */ public function accessRules() { return array(); } /** * Runs the named action. * Filters specified via {@link filters()} will be applied. * @param string action ID * @throws CHttpException if the action does not exist or the action name is not proper. * @see filters * @see createAction * @see runAction */ public function run($actionID) { if(($action=$this->createAction($actionID))!==null) $this->runActionWithFilters($action,$this->filters()); else $this->missingAction($actionID); } /** * Runs an action with the specified filters. * A filter chain will be created based on the specified filters * and the action will be executed then. * @param CAction the action to be executed. * @param array list of filters to be applied to the action. * @see filters * @see createAction * @see run */ public function runActionWithFilters($action,$filters=array()) { if(!empty($filters)) { $priorAction=$this->_action; $this->_action=$action; CFilterChain::create($this,$action,$filters)->run(); $this->_action=$priorAction; } else $this->runAction($action); } /** * Runs the action after passing through all filters. * This method is invoked by {@link runAction} after all possible filters have been executed * and the action starts to run. * @param CAction action to run */ public function runAction($action) { $priorAction=$this->_action; $this->_action=$action; if($this->beforeAction($action)) { $action->run(); $this->afterAction($action); } $this->_action=$priorAction; } /** * Postprocesses the output generated by {@link render()}. * This method is invoked at the end of {@link render()}. * If there are registered client scripts, this method will insert them into the output * at appropriate places. If there are dynamic contents, they will also be inserted. * @param string the output generated by the current action * @return string the output that has been processed. */ protected function processOutput($output) { if($this->_clientScript) { $output=$this->_clientScript->render($output); $this->_clientScript=null; } if($this->_dynamicOutput) { $output=preg_replace_callback('/<###tmp(\d+)###>/',array($this,'replaceDynamicOutput'),$output); $this->_dynamicOutput=null; } return $output; } /** * Replaces the dynamic content placeholders with actual content. * This is a callback function used internally. * @param array matches * @return string the replacement * @see processOutput */ protected function replaceDynamicOutput($matches) { return isset($this->_dynamicOutput[$matches[1]]) ? $this->_dynamicOutput[$matches[1]] : $matches[0]; } /** * Creates the action instance based on the action name. * The action can be either an inline action or an object. * The latter is created by looking up the action map specified in {@link actions}. * @param string ID of the action. If empty, the {@link defaultAction default action} will be used. * @return CAction the action instance, null if the action does not exist. * @see actions */ public function createAction($actionID) { if($actionID==='') $actionID=$this->defaultAction; if(method_exists($this,'action'.$actionID) && strcasecmp($actionID,'s')) // we have actions method return new CInlineAction($this,$actionID); $actionMap=$this->actions(); if(isset($actionMap[$actionID])) { $c=$actionMap[$actionID]; if(is_string($c)) { $className=Yii::import($c,true); return new $className($this,$actionID); } else return CConfiguration::createObject($c,$this,$actionID); } return null; } /** * Handles the request whose action is not recognized. * This method is invoked when the controller cannot find the requested action. * The default implementation simply throws an exception. * @param string the missing action name * @throws CHttpException whenever this method is invoked */ public function missingAction($actionID) { throw new CHttpException(404,Yii::t('yii#The system is unable to find the requested action "{action}".', array('{action}'=>$actionID==''?$this->defaultAction:$actionID))); } /** * @return CAction the action currently being executed, null if no active action. */ public function getAction() { return $this->_action; } /** * @param CAction the action currently being executed. */ public function setAction($value) { $this->_action=$value; } /** * @return string id of the controller */ public function getId() { return $this->_id; } /** * Returns the directory containing view files for this controller. * The default implementation returns 'protected/views/ControllerID'. * Child classes may override this method to use customized view path. * @return string the directory containing the view files for this controller. Defaults to 'protected/views/ControllerID'. */ public function getViewPath() { return Yii::app()->getViewPath().DIRECTORY_SEPARATOR.$this->getId(); } /** * Looks for the view file according to the view name. * This method will look for the view under the controller's {@link getViewPath viewPath}. * If the view name starts with '/', the view will be looked for under the application's * {@link CWebApplication::getViewPath viewPath}. * The view script file is named as "ViewName.php". A localized view file * may be returned if internationalization is needed. See {@link CApplication::findLocalizedFile} * for more details. * @param string name of the view (without file extension) * @return string the view file path, false if the view file does not exist * @see CApplication::findLocalizedFile */ public function getViewFile($viewName) { if(($theme=Yii::app()->getTheme())!==null && ($viewFile=$theme->getViewFile($this,$viewName))!==false) return $viewFile; if($viewName[0]==='/') $viewFile=Yii::app()->getViewPath().$viewName.'.php'; else $viewFile=$this->getViewPath().DIRECTORY_SEPARATOR.$viewName.'.php'; return is_file($viewFile) ? Yii::app()->findLocalizedFile($viewFile) : false; } /** * Looks for the view script file for a layout. * This method will look for the view under the application's {@link CWebApplication::getLayoutPath layoutPath}. * If the view name starts with '/', the view will be looked for under the application's * {@link CWebApplication::getViewPath viewPath}. * @param string layout name * @return string the view file for the layout. False if the view file cannot be found */ public function getLayoutFile($layoutName) { if(($theme=Yii::app()->getTheme())!==null && ($layoutFile=$theme->getLayoutFile($this,$layoutName))!==false) return $layoutFile; if($layoutName[0]==='/') $layoutFile=Yii::app()->getViewPath().$layoutName.'.php'; else $layoutFile=Yii::app()->getLayoutPath().DIRECTORY_SEPARATOR.$layoutName.'.php'; return is_file($layoutFile) ? Yii::app()->findLocalizedFile($layoutFile) : false; } /** * Returns the list of clips. * A clip is a named piece of rendering result that can be * inserted at different places. * @return CMap the list of clips * @see CClipWidget */ public function getClips() { if($this->_clips!==null) return $this->_clips; else return $this->_clips=new CMap; } /** * Renders a view with a layout. * * This method first calls {@link renderPartial} to render the view (called content view). * It then renders the layout view which may embed the content view at appropriate place. * In the layout view, the content view rendering result can be accessed via variable * $content. At the end, it calls {@link processOutput} to insert scripts * and dynamic contents if they are available. * * By default, the layout view script is "protected/views/layouts/main.php". * This may be customized by changing {@link layout}. * * @param string name of the view to be rendered. See {@link getViewFile} for details * about how the view script is resolved. * @param array data to be extracted into PHP variables and made available to the view script * @param boolean whether the rendering result should be returned instead of being displayed to end users. * @return string the rendering result. Null if the rendering result is not required. * @see renderPartial * @see getLayoutFile */ public function render($view,$data=null,$return=false) { if(($layout=$this->layout)==null) $layout=Yii::app()->layout; $output=$this->renderPartial($view,$data,true); if(!empty($layout) && ($layoutFile=$this->getLayoutFile($layout))!==false) $output=$this->renderFile($layoutFile,array('content'=>$output),true); $output=$this->processOutput($output); if($return) return $output; else echo $output; } /** * Renders a static text string. * The string will be inserted in the current controller layout and returned back. * @param string the static text string * @param boolean whether the rendering result should be returned instead of being displayed to end users. * @return string the rendering result. Null if the rendering result is not required. * @see getLayoutFile */ public function renderText($text,$return=false) { if(($layout=$this->layout)==null) $layout=Yii::app()->layout; if(!empty($layout) && ($layoutFile=$this->getLayoutFile($layout))!==false) $output=$this->renderFile($layoutFile,array('content'=>$text),true); $output=$this->processOutput($output); if($return) return $output; else echo $output; } /** * Renders a view. * * The named view refers to a PHP script (resolved via {@link getViewFile}) * that is included by this method. If $data is an associative array, * it will be extracted as PHP variables and made available to the script. * * This method differs from {@link render()} in that it does not * apply a layout to the rendered result. It is thus mostly used * in rendering a partial view, or an AJAX response. * * @param string name of the view to be rendered. See {@link getViewFile} for details * about how the view script is resolved. * @param array data to be extracted into PHP variables and made available to the view script * @param boolean whether the rendering result should be returned instead of being displayed to end users * @return string the rendering result. Null if the rendering result is not required. * @throws CException if the view does not exist * @see getViewFile * @see render */ public function renderPartial($view,$data=null,$return=false) { if(($viewFile=$this->getViewFile($view))!==false) return $this->renderFile($viewFile,$data,$return); else throw new CException(Yii::t('yii#{controller} cannot find the requested view "{view}".', array('{controller}'=>get_class($this), '{view}'=>$view))); } /** * Renders dynamic content returned by the specified callback. * This method is used together with {@link COutputCache}. Dynamic contents * will always show as their latest state even if the content surrounding them is being cached. * This is especially useful when caching pages that are mostly static but contain some small * dynamic regions, such as username or current time. * We can use this method to render these dynamic regions to ensure they are always up-to-date. * * Except the first parameters, parameters of this method will be passed to the specified callback. * * Note, the callback and its parameter values will be serialized and saved in cache. * Make sure they are serializable. * * @param callback a PHP callback which returns the needed dynamic content. * When the callback is specified as a string, it will be first assumed to be a method of the current * controller class. If the method does not exist, it is assumed to be a global PHP function. * Note, the callback should return the dynamic content instead of echoing it. */ public function renderDynamic($callback) { $n=count($this->_dynamicOutput); echo "<###tmp$n###>"; $params=func_get_args(); array_shift($params); $this->renderDynamicInternal($callback,$params); } /** * This method is internally used. * @param callback a PHP callback which returns the needed dynamic content. * @param array parameters passed to the PHP callback * @see renderDynamic */ public function renderDynamicInternal($callback,$params) { if(is_string($callback) && method_exists($this,$callback)) $callback=array($this,$callback); $this->_dynamicOutput[]=call_user_func_array($callback,$params); $this->recordCachingAction('','renderDynamicInternal',array($callback,$params)); } /** * @return CClientScript the client script manager */ public function getClientScript() { if($this->_clientScript) return $this->_clientScript; else return $this->_clientScript=new CClientScript($this); } /** * Creates a relative URL for the specified action defined in this controller. * @param string the URL route. This should be in the format of 'ControllerID/ActionID'. * If the ControllerPath is not present, the current controller ID will be prefixed to the route. * If the route is empty, it is assumed to be the current action. * @param array additional GET parameters (name=>value). Both the name and value will be URL-encoded. * @param string the token separating name-value pairs in the URL. * @return string the constructed URL */ public function createUrl($route,$params=array(),$ampersand='&') { if(strpos($route,'/')===false) $route=$this->getId().'/'.($route==='' ? $this->getAction()->getId() : $route); return Yii::app()->createUrl($route,$params,$ampersand); } /** * @return string the page title. Defaults to the controller name and the action name. */ public function getPageTitle() { if($this->_pageTitle!==null) return $this->_pageTitle; else { if($this->getAction()!==null && strcasecmp($this->getAction()->getId(),$this->defaultAction)) return $this->_pageTitle=Yii::app()->name.' - '.ucfirst($this->getAction()->getId()).' '.ucfirst($this->getId()); else return $this->_pageTitle=Yii::app()->name.' - '.ucfirst($this->getId()); } } /** * @param string the page title. */ public function setPageTitle($value) { $this->_pageTitle=$value; } /** * Redirects the browser to the specified URL or route (controller/action). * @param mixed the URL to be redirected to. If the parameter is an array, * the first element must be a route to a controller action and the rest * are GET parameters in name-value pairs. * @param boolean whether to terminate the current application after calling this method */ public function redirect($url,$terminate=true) { if(is_array($url)) { $route=isset($url[0]) ? $url[0] : ''; $url=$this->createUrl($route,array_splice($url,1)); } Yii::app()->getRequest()->redirect($url,$terminate); } /** * Refreshes the current page. * The effect of this method call is the same as user pressing the * refresh button on the browser (without post data). * @param boolean whether to terminate the current application after calling this method **/ public function refresh($terminate=true) { $this->redirect(Yii::app()->getRequest()->getUrl(),$terminate); } /** * Records a method call when an output cache is in effect. * When the content is served from the output cache, the recorded * method will be re-invoked. * @param string a property name of the controller. It refers to an object * whose method is being called. If empty it means the controller itself. * @param string the method name * @param array parameters passed to the method * @see COutputCache */ public function recordCachingAction($context,$method,$params) { if($this->_cachingStack) // record only when there is an active output cache { foreach($this->_cachingStack as $cache) $cache->recordAction($context,$method,$params); } } /** * @return CStack stack of {@link COutputCache} objects */ public function getCachingStack() { if(!$this->_cachingStack) $this->_cachingStack=new CStack; return $this->_cachingStack; } /** * This method is invoked right before an action is to be executed (after all possible filters.) * You may override this method to do last-minute preparation for the action. * @param CAction the action to be executed. * @return boolean whether the action should be executed. */ protected function beforeAction($action) { return true; } /** * This method is invoked right after an action is executed. * You may override this method to do some postprocessing for the action. * @param CAction the action just executed. */ protected function afterAction($action) { } /** * The filter method for 'postOnly' filter. * This filter reports an error if the applied action is receiving a non-POST request. * @param CFilterChain the filter chain that the filter is on. * @throws CHttpException if the current request is not a POST request */ public function filterPostOnly($filterChain) { if(Yii::app()->getRequest()->getIsPostRequest()) $filterChain->run(); else throw new CHttpException(400,Yii::t('yii#Your request is not valid.')); } /** * The filter method for 'ajaxOnly' filter. * This filter reports an error if the applied action is receiving a non-AJAX request. * @param CFilterChain the filter chain that the filter is on. * @throws CHttpException if the current request is not an AJAX request. */ public function filterAjaxOnly($filterChain) { if(Yii::app()->getRequest()->getIsAjaxRequest()) $filterChain->run(); else throw new CHttpException(400,Yii::t('yii#Your request is not valid.')); } /** * The filter method for 'accessControl' filter. * This filter is a wrapper of {@link CAccessControlFilter}. * To use this filter, you must override {@link accessRules} method. * @param CFilterChain the filter chain that the filter is on. */ public function filterAccessControl($filterChain) { $filter=new CAccessControlFilter; $filter->setRules($this->accessRules()); $filter->filter($filterChain); } /** * Generates pagination information. * This method can be used to generate pagination information given item count * and page size. The pagination information can then be passed to {@link CBasePager pagers} * for corresponding rendering. * @param integer the total item count * @param integer the page size. See {@link CPagination} for default value. * @param string the name of the GET variable storing the current page index. See {@link CPagination} for default value. * @return CPagination the pagination information */ protected function paginate($itemCount,$pageSize=null,$pageVar=null) { $pages=new CPagination; $pages->setItemCount($itemCount); if($pageSize!==null) $pages->pageSize=$pageSize; if($pageVar!==null) $pages->pageVar=$pageVar; return $pages; } }