0 follower

CBaseController

Package system.web
Inheritance abstract class CBaseController » CComponent
Subclasses CController, CWidget
Since 1.0
Version $Id$
Source Code 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'];


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

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__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
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() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
createWidget() Creates a widget and initializes it. CBaseController
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
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. 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
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getViewFile() Returns the view script file according to the specified view name. CBaseController
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
raiseEvent() Raises an event. CComponent
renderFile() Renders a view file. CBaseController
renderInternal() Renders a view file. CBaseController
widget() Creates a widget and executes it. CBaseController

Method Details

beginCache() method
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.
Source Code: framework/web/CBaseController.php#233 (show)
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();
}

See Also

beginClip() method
public void beginClip(string $id, array $properties=array ( ))
$id string the clip ID.
$properties array initial property values for CClipWidget.
Source Code: framework/web/CBaseController.php#200 (show)
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() method
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. This parameter has been available since version 1.0.4
Source Code: framework/web/CBaseController.php#270 (show)
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() method
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
Source Code: framework/web/CBaseController.php#167 (show)
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.

See Also

createWidget() method
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.
Source Code: framework/web/CBaseController.php#135 (show)
public function createWidget($className,$properties=array())
{
    
$className=Yii::import($className,true);
    
$widget=new $className($this);
    foreach(
$properties as $name=>$value)
        
$widget->$name=$value;
    
$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.

endCache() method
public void endCache()
Source Code: framework/web/CBaseController.php#251 (show)
public function endCache()
{
    
$this->endWidget('COutputCache');
}

Ends fragment caching. This is an alias to endWidget.

See Also

endClip() method
public void endClip()
Source Code: framework/web/CBaseController.php#210 (show)
public function endClip()
{
    
$this->endWidget('CClipWidget');
}

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

endContent() method
public void endContent()
Source Code: framework/web/CBaseController.php#279 (show)
public function endContent()
{
    
$this->endWidget('CContentDecorator');
}

Ends the rendering of content.

See Also

endWidget() method
public CWidget endWidget(string $id='')
$id string optional tag identifying the method call for debugging purpose.
{return} CWidget the widget just ended running
Source Code: framework/web/CBaseController.php#182 (show)
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().

See Also

getViewFile() method
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.
Source Code: framework/web/CBaseController.php#70 (show)
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() method
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.
Source Code: framework/web/CBaseController.php#82 (show)
public function renderFile($viewFile,$data=null,$return=false)
{
    
$widgetCount=count($this->_widgetStack);
    if((
$renderer=Yii::app()->getViewRenderer())!==null)
        
$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() method
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.
Source Code: framework/web/CBaseController.php#108 (show)
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() method
public CWidget widget(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 instance. This return value has been available since version 1.0.4.
Source Code: framework/web/CBaseController.php#151 (show)
public function widget($className,$properties=array())
{
    
$widget=$this->createWidget($className,$properties);
    
$widget->run();
    return 
$widget;
}

Creates a widget and executes it.