Yii Framework v1.1.10 绫诲弬鑰

CBaseController

system.web
缁ф壙 abstract class CBaseController » CComponent
瀛愮被 CController, CWidget
婧愯嚜 1.0
鐗堟湰 $Id: CBaseController.php 3515 2011-12-28 12:29:24Z mdomba $
婧愮爜 framework/web/CBaseController.php
CBaseController is the base class for CController and CWidget.

It provides the common functionalities shared by controllers who need to render views.

CBaseController also implements the support for the following features:
  • Clips : a clip is a piece of captured output that can be inserted elsewhere.
  • Widgets : a widget is a self-contained sub-controller with its own view and model.
  • Fragment cache : fragment cache selectively caches a portion of the output.


To use a widget in a view, use the following in the view:
$this->widget('path.to.widgetClass',array('property1'=>'value1',...));
or
$this->beginWidget('path.to.widgetClass',array('property1'=>'value1',...));
// ... display other contents here
$this->endWidget();


To create a clip, use the following:
$this->beginClip('clipID');
// ... display the clip contents
$this->endClip();
Then, in a different view or place, the captured clip can be inserted as:
echo $this->clips['clipID'];


Note that $this in the code above refers to current controller so, for example, if you need to access clip from a widget where $this refers to widget itself you need to do it the following way:

echo $this->getController()->clips['clipID'];


To use fragment cache, do as follows,
if($this->beginCache('cacheID',array('property1'=>'value1',...))
{
    // ... display the content to be cached here
   $this->endCache();
}

鍏叡鏂规硶

闅愯棌缁ф壙鏂规硶

鏂规硶鎻忚堪瀹氫箟鍦
__call() 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 CComponent
__get() 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 CComponent
__isset() 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 CComponent
__set() 璁剧疆涓涓粍浠剁殑灞炴у笺 CComponent
__unset() 璁剧疆涓涓粍浠剁殑灞炴т负null銆 CComponent
asa() 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° CComponent
attachBehavior() 闄勫姞涓涓涓哄埌缁勪欢銆 CComponent
attachBehaviors() 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 CComponent
attachEventHandler() 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 CComponent
beginCache() Begins fragment caching. CBaseController
beginClip() Begins recording a clip. CBaseController
beginContent() Begins the rendering of content that is to be decorated by the specified view. CBaseController
beginWidget() Creates a widget and executes it. CBaseController
canGetProperty() 纭畾灞炴ф槸鍚﹀彲璇汇 CComponent
canSetProperty() 纭畾灞炴ф槸鍚﹀彲鍐欍 CComponent
createWidget() Creates a widget and initializes it. CBaseController
detachBehavior() 浠庣粍浠朵腑鍒嗙涓涓涓恒 CComponent
detachBehaviors() 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 CComponent
detachEventHandler() 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 CComponent
disableBehavior() 绂佺敤涓涓檮鍔犺涓恒 CComponent
disableBehaviors() 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
enableBehavior() 鍚敤涓涓檮鍔犺涓恒 CComponent
enableBehaviors() 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 CComponent
endCache() Ends fragment caching. CBaseController
endClip() Ends recording a clip. CBaseController
endContent() Ends the rendering of content. CBaseController
endWidget() Ends the execution of the named widget. CBaseController
evaluateExpression() 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 CComponent
getEventHandlers() 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 CComponent
getViewFile() Returns the view script file according to the specified view name. CBaseController
hasEvent() 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 CComponent
hasEventHandler() 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 CComponent
hasProperty() 纭畾灞炴ф槸鍚﹁瀹氫箟銆 CComponent
raiseEvent() 鍙戣捣涓涓簨浠躲 CComponent
renderFile() Renders a view file. CBaseController
renderInternal() Renders a view file. CBaseController
widget() Creates a widget and executes it. CBaseController

鏂规硶璇︾粏

beginCache() 鏂规硶
public boolean beginCache(string $id, array $properties=array ( ))
$id string a unique ID identifying the fragment to be cached.
$properties array initial property values for COutputCache.
{return} boolean whether we need to generate content for caching. False if cached version is available.
婧愮爜锛 framework/web/CBaseController.php#254 (鏄剧ず)
public function beginCache($id,$properties=array())
{
    
$properties['id']=$id;
    
$cache=$this->beginWidget('COutputCache',$properties);
    if(
$cache->getIsContentCached())
    {
        
$this->endCache();
        return 
false;
    }
    else
        return 
true;
}

Begins fragment caching. This method will display cached content if it is availabe. If not, it will start caching and would expect a endCache() call to end the cache and save the content into cache. A typical usage of fragment caching is as follows,

if($this->beginCache($id))
{
    // ...generate content here
    $this->endCache();
}

鍙傝

beginClip() 鏂规硶
public void beginClip(string $id, array $properties=array ( ))
$id string the clip ID.
$properties array initial property values for CClipWidget.
婧愮爜锛 framework/web/CBaseController.php#221 (鏄剧ず)
public function beginClip($id,$properties=array())
{
    
$properties['id']=$id;
    
$this->beginWidget('CClipWidget',$properties);
}

Begins recording a clip. This method is a shortcut to beginning CClipWidget.

beginContent() 鏂规硶
public void beginContent(mixed $view=NULL, array $data=array ( ))
$view mixed the name of the view that will be used to decorate the content. The actual view script is resolved via getViewFile. If this parameter is null (default), the default layout will be used as the decorative view. Note that if the current controller does not belong to any module, the default layout refers to the application's default layout; If the controller belongs to a module, the default layout refers to the module's default layout.
$data array the variables (name=>value) to be extracted and made available in the decorative view.
婧愮爜锛 framework/web/CBaseController.php#290 (鏄剧ず)
public function beginContent($view=null,$data=array())
{
    
$this->beginWidget('CContentDecorator',array('view'=>$view'data'=>$data));
}

Begins the rendering of content that is to be decorated by the specified view.

beginWidget() 鏂规硶
public CWidget beginWidget(string $className, array $properties=array ( ))
$className string the widget class name or class in dot syntax (e.g. application.widgets.MyWidget)
$properties array list of initial property values for the widget (Property Name => Property Value)
{return} CWidget the widget created to run
婧愮爜锛 framework/web/CBaseController.php#188 (鏄剧ず)
public function beginWidget($className,$properties=array())
{
    
$widget=$this->createWidget($className,$properties);
    
$this->_widgetStack[]=$widget;
    return 
$widget;
}

Creates a widget and executes it. This method is similar to widget() except that it is expecting a endWidget() call to end the execution.

鍙傝

createWidget() 鏂规硶
public CWidget createWidget(string $className, array $properties=array ( ))
$className string class name (can be in path alias format)
$properties array initial property values
{return} CWidget the fully initialized widget instance.
婧愮爜锛 framework/web/CBaseController.php#145 (鏄剧ず)
public function createWidget($className,$properties=array())
{
    
$widget=Yii::app()->getWidgetFactory()->createWidget($this,$className,$properties);
    
$widget->init();
    return 
$widget;
}

Creates a widget and initializes it. This method first creates the specified widget instance. It then configures the widget's properties with the given initial values. At the end it calls CWidget::init to initialize the widget. Starting from version 1.1, if a widget factory is enabled, this method will use the factory to create the widget, instead.

endCache() 鏂规硶
public void endCache()
婧愮爜锛 framework/web/CBaseController.php#272 (鏄剧ず)
public function endCache()
{
    
$this->endWidget('COutputCache');
}

Ends fragment caching. This is an alias to endWidget.

鍙傝

endClip() 鏂规硶
public void endClip()
婧愮爜锛 framework/web/CBaseController.php#231 (鏄剧ず)
public function endClip()
{
    
$this->endWidget('CClipWidget');
}

Ends recording a clip. This method is an alias to endWidget.

endContent() 鏂规硶
public void endContent()
婧愮爜锛 framework/web/CBaseController.php#299 (鏄剧ず)
public function endContent()
{
    
$this->endWidget('CContentDecorator');
}

Ends the rendering of content.

鍙傝

endWidget() 鏂规硶
public CWidget endWidget(string $id='')
$id string optional tag identifying the method call for debugging purpose.
{return} CWidget the widget just ended running
婧愮爜锛 framework/web/CBaseController.php#203 (鏄剧ず)
public function endWidget($id='')
{
    if((
$widget=array_pop($this->_widgetStack))!==null)
    {
        
$widget->run();
        return 
$widget;
    }
    else
        throw new 
CException(Yii::t('yii','{controller} has an extra endWidget({id}) call in its view.',
            array(
'{controller}'=>get_class($this),'{id}'=>$id)));
}

Ends the execution of the named widget. This method is used together with beginWidget().

鍙傝

getViewFile() 鏂规硶
abstract public string getViewFile(string $viewName)
$viewName string view name
{return} string the file path for the named view. False if the view cannot be found.
婧愮爜锛 framework/web/CBaseController.php#78 (鏄剧ず)
abstract public function getViewFile($viewName);

Returns the view script file according to the specified view name. This method must be implemented by child classes.

renderFile() 鏂规硶
public string renderFile(string $viewFile, array $data=NULL, boolean $return=false)
$viewFile string view file path
$data array data to be extracted and made available to the view
$return boolean whether the rendering result should be returned instead of being echoed
{return} string the rendering result. Null if the rendering result is not required.
婧愮爜锛 framework/web/CBaseController.php#90 (鏄剧ず)
public function renderFile($viewFile,$data=null,$return=false)
{
    
$widgetCount=count($this->_widgetStack);
    if((
$renderer=Yii::app()->getViewRenderer())!==null && $renderer->fileExtension==='.'.CFileHelper::getExtension($viewFile))
        
$content=$renderer->renderFile($this,$viewFile,$data,$return);
    else
        
$content=$this->renderInternal($viewFile,$data,$return);
    if(
count($this->_widgetStack)===$widgetCount)
        return 
$content;
    else
    {
        
$widget=end($this->_widgetStack);
        throw new 
CException(Yii::t('yii','{controller} contains improperly nested widget tags in its view "{view}". A {widget} widget does not have an endWidget() call.',
            array(
'{controller}'=>get_class($this), '{view}'=>$viewFile'{widget}'=>get_class($widget))));
    }
}

Renders a view file.

renderInternal() 鏂规硶
public string renderInternal(string $_viewFile_, array $_data_=NULL, boolean $_return_=false)
$_viewFile_ string view file
$_data_ array data to be extracted and made available to the view file
$_return_ boolean whether the rendering result should be returned as a string
{return} string the rendering result. Null if the rendering result is not required.
婧愮爜锛 framework/web/CBaseController.php#116 (鏄剧ず)
public function renderInternal($_viewFile_,$_data_=null,$_return_=false)
{
    
// we use special variable names here to avoid conflict when extracting data
    
if(is_array($_data_))
        
extract($_data_,EXTR_PREFIX_SAME,'data');
    else
        
$data=$_data_;
    if(
$_return_)
    {
        
ob_start();
        
ob_implicit_flush(false);
        require(
$_viewFile_);
        return 
ob_get_clean();
    }
    else
        require(
$_viewFile_);
}

Renders a view file. This method includes the view file as a PHP script and captures the display result if required.

widget() 鏂规硶
public mixed widget(string $className, array $properties=array ( ), boolean $captureOutput=false)
$className string the widget class name or class in dot syntax (e.g. application.widgets.MyWidget)
$properties array list of initial property values for the widget (Property Name => Property Value)
$captureOutput boolean whether to capture the output of the widget. If true, the method will capture and return the output generated by the widget. If false, the output will be directly sent for display and the widget object will be returned. This parameter is available since version 1.1.2.
{return} mixed the widget instance when $captureOutput is false, or the widget output when $captureOutput is true.
婧愮爜锛 framework/web/CBaseController.php#161 (鏄剧ず)
public function widget($className,$properties=array(),$captureOutput=false)
{
    if(
$captureOutput)
    {
        
ob_start();
        
ob_implicit_flush(false);
        
$widget=$this->createWidget($className,$properties);
        
$widget->run();
        return 
ob_get_clean();
    }
    else
    {
        
$widget=$this->createWidget($className,$properties);
        
$widget->run();
        return 
$widget;
    }
}

Creates a widget and executes it.

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.