CBaseController

优质
小牛编辑
121浏览
2023-12-01
所有包 | 方法
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()计算一个PHP表达式,或根据组件上下文执行回调。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 ( ))
$idstringa unique ID identifying the fragment to be cached.
$propertiesarrayinitial property values for COutputCache.
{return}booleanwhether we need to generate content for caching. False if cached version is available.
源码: framework/web/CBaseController.php#254 (显示) publicfunctionbeginCache($id,$properties=array())
{
$properties['id']=$id;
$cache=$this->beginWidget('COutputCache',$properties);
if($cache->getIsContentCached())
{
$this->endCache();
returnfalse;
}
else
returntrue;
}

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();
}

参见

  • endCache
beginClip() 方法
public void beginClip(string $id, array $properties=array ( ))
$idstringthe clip ID.
$propertiesarrayinitial property values for CClipWidget.
源码: framework/web/CBaseController.php#221 (显示) publicfunctionbeginClip($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 ( ))
$viewmixedthe 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.
$dataarraythe variables (name=>value) to be extracted and made available in the decorative view.
源码: framework/web/CBaseController.php#290 (显示) publicfunctionbeginContent($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.

参见

  • endContent
  • CContentDecorator
beginWidget() 方法
public CWidget beginWidget(string $className, array $properties=array ( ))
$classNamestringthe widget class name or class in dot syntax (e.g. application.widgets.MyWidget)
$propertiesarraylist of initial property values for the widget (Property Name => Property Value)
{return}CWidgetthe widget created to run
源码: framework/web/CBaseController.php#188 (显示) publicfunctionbeginWidget($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.

参见

  • endWidget
createWidget() 方法
public CWidget createWidget(string $className, array $properties=array ( ))
$classNamestringclass name (can be in path alias format)
$propertiesarrayinitial property values
{return}CWidgetthe fully initialized widget instance.
源码: framework/web/CBaseController.php#145 (显示) publicfunctioncreateWidget($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 (显示) publicfunctionendCache()
{
$this->endWidget('COutputCache');
}

Ends fragment caching. This is an alias to endWidget.

参见

  • beginCache
endClip() 方法
public void endClip()
源码: framework/web/CBaseController.php#231 (显示) publicfunctionendClip()
{
$this->endWidget('CClipWidget');
}

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

endContent() 方法
public void endContent()
源码: framework/web/CBaseController.php#299 (显示) publicfunctionendContent()
{
$this->endWidget('CContentDecorator');
}

Ends the rendering of content.

参见

  • beginContent
endWidget() 方法
public CWidget endWidget(string $id='')
$idstringoptional tag identifying the method call for debugging purpose.
{return}CWidgetthe widget just ended running
源码: framework/web/CBaseController.php#203 (显示) publicfunctionendWidget($id='')
{
if(($widget=array_pop($this->_widgetStack))!==null)
{
$widget->run();
return$widget;
}
else
thrownewCException(Yii::t('yii','{controller}hasanextraendWidget({id})callinitsview.',
array('{controller}'=>get_class($this),'{id}'=>$id)));
}

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

参见

  • beginWidget
getViewFile() 方法
abstract public string getViewFile(string $viewName)
$viewNamestringview name
{return}stringthe file path for the named view. False if the view cannot be found.
源码: framework/web/CBaseController.php#78 (显示) abstractpublicfunctiongetViewFile($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)
$viewFilestringview file path
$dataarraydata to be extracted and made available to the view
$returnbooleanwhether the rendering result should be returned instead of being echoed
{return}stringthe rendering result. Null if the rendering result is not required.
源码: framework/web/CBaseController.php#90 (显示) publicfunctionrenderFile($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);
thrownewCException(Yii::t('yii','{controller}containsimproperlynestedwidgettagsinitsview"{view}".A{widget}widgetdoesnothaveanendWidget()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_stringview file
$_data_arraydata to be extracted and made available to the view file
$_return_booleanwhether the rendering result should be returned as a string
{return}stringthe rendering result. Null if the rendering result is not required.
源码: framework/web/CBaseController.php#116 (显示) publicfunctionrenderInternal($_viewFile_,$_data_=null,$_return_=false)
{
//weusespecialvariablenamesheretoavoidconflictwhenextractingdata
if(is_array($_data_))
extract($_data_,EXTR_PREFIX_SAME,'data');
else
$data=$_data_;
if($_return_)
{
ob_start();
ob_implicit_flush(false);
require($_viewFile_);
returnob_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)
$classNamestringthe widget class name or class in dot syntax (e.g. application.widgets.MyWidget)
$propertiesarraylist of initial property values for the widget (Property Name => Property Value)
$captureOutputbooleanwhether 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}mixedthe widget instance when $captureOutput is false, or the widget output when $captureOutput is true.
源码: framework/web/CBaseController.php#161 (显示) publicfunctionwidget($className,$properties=array(),$captureOutput=false)
{
if($captureOutput)
{
ob_start();
ob_implicit_flush(false);
$widget=$this->createWidget($className,$properties);
$widget->run();
returnob_get_clean();
}
else
{
$widget=$this->createWidget($className,$properties);
$widget->run();
return$widget;
}
}

Creates a widget and executes it.