system system.base system.caching system.caching.dependencies system.collections system.console system.db system.db.ar system.db.schema system.db.schema.cubrid system.db.schema.mssql system.db.schema.mysql system.db.schema.oci system.db.schema.pgsql system.db.schema.sqlite system.gii system.i18n system.i18n.gettext system.logging system.test system.utils system.validators system.web system.web.actions system.web.auth system.web.filters system.web.form system.web.helpers system.web.renderers system.web.services system.web.widgets system.web.widgets.captcha system.web.widgets.pagers zii.behaviors zii.widgets zii.widgets.grid zii.widgets.jui

CViewAction

system.web.actions
继承 class CViewAction » CAction » CComponent
实现 IAction
可用自 1.0
源码 framework/web/actions/CViewAction.php
CViewAction represents an action that displays a view according to a user-specified parameter.

By default, the view being displayed is specified via the view GET parameter. The name of the GET parameter can be customized via viewParam. If the user doesn't provide the GET parameter, the default view specified by defaultView will be displayed.

Users specify a view in the format of path.to.view, which translates to the view name BasePath/path/to/view where BasePath is given by basePath.

Note, the user specified view can only contain word characters, dots and dashes and the first letter must be a word letter.

公共属性

隐藏继承的属性

属性类型描述被定义在
basePath string the base path for the views. CViewAction
controller CController the controller who owns this action. CAction
defaultView string the name of the default view when viewParam GET parameter is not provided by user. CViewAction
id string id of this action CAction
layout mixed the name of the layout to be applied to the views. CViewAction
renderAsText boolean whether the view should be rendered as PHP script or static text. CViewAction
requestedView string Returns the name of the view requested by the user. CViewAction
view string the name of the view to be rendered. CViewAction
viewParam string the name of the GET parameter that contains the requested view name. CViewAction

公共方法

隐藏继承的方法

方法描述被定义在
__call() Calls the named method which is not a class method. CComponent
__construct() Constructor. CAction
__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
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
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
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getController() Returns the controller who owns this action. CAction
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getId() Returns id of this action CAction
getRequestedView() Returns the name of the view requested by the user. CViewAction
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
onAfterRender() Raised right after the action invokes the render method. CViewAction
onBeforeRender() Raised right before the action invokes the render method. CViewAction
raiseEvent() Raises an event. CComponent
run() Runs the action. CViewAction
runWithParams() Runs the action with the supplied request parameters. CAction

受保护的方法

隐藏继承的方法

方法描述被定义在
resolveView() Resolves the user-specified view into a valid view name. CViewAction
runWithParamsInternal() Executes a method of an object with the supplied named parameters. CAction

事件

隐藏继承的事件

事件描述被定义在
onBeforeRender Raised right before the action invokes the render method. CViewAction
onAfterRender Raised right after the action invokes the render method. CViewAction

属性详情

basePath 属性
public string $basePath;

the base path for the views. Defaults to 'pages'. The base path will be prefixed to any user-specified page view. For example, if a user requests for tutorial.chap1, the corresponding view name will be pages/tutorial/chap1, assuming the base path is pages. The actual view file is determined by CController::getViewFile.

defaultView 属性
public string $defaultView;

the name of the default view when viewParam GET parameter is not provided by user. Defaults to 'index'. This should be in the format of 'path.to.view', similar to that given in the GET parameter.

参见

layout 属性
public mixed $layout;

the name of the layout to be applied to the views. This will be assigned to CController::layout before the view is rendered. Defaults to null, meaning the controller's layout will be used. If false, no layout will be applied.

renderAsText 属性
public boolean $renderAsText;

whether the view should be rendered as PHP script or static text. Defaults to false.

requestedView 属性 只读
public string getRequestedView()

Returns the name of the view requested by the user. If the user doesn't specify any view, the defaultView will be returned.

view 属性
public string $view;

the name of the view to be rendered. This property will be set once the user requested view is resolved.

viewParam 属性
public string $viewParam;

the name of the GET parameter that contains the requested view name. Defaults to 'view'.

方法详情

getRequestedView() 方法
public string getRequestedView()
{return} string the name of the view requested by the user. This is in the format of 'path.to.view'.
源码: framework/web/actions/CViewAction.php#80 (显示)
public function getRequestedView()
{
    if(
$this->_viewPath===null)
    {
        if(!empty(
$_GET[$this->viewParam]) && is_string($_GET[$this->viewParam]))
            
$this->_viewPath=$_GET[$this->viewParam];
        else
            
$this->_viewPath=$this->defaultView;
    }
    return 
$this->_viewPath;
}

Returns the name of the view requested by the user. If the user doesn't specify any view, the defaultView will be returned.

onAfterRender() 方法
public void onAfterRender(CEvent $event)
$event CEvent event parameter
源码: framework/web/actions/CViewAction.php#163 (显示)
public function onAfterRender($event)
{
    
$this->raiseEvent('onAfterRender',$event);
}

Raised right after the action invokes the render method.

onBeforeRender() 方法
public void onBeforeRender(CEvent $event)
$event CEvent event parameter
源码: framework/web/actions/CViewAction.php#154 (显示)
public function onBeforeRender($event)
{
    
$this->raiseEvent('onBeforeRender',$event);
}

Raised right before the action invokes the render method. Event handlers can set the CEvent::handled property to be true to stop further view rendering.

resolveView() 方法
protected string resolveView(string $viewPath)
$viewPath string user-specified view in the format of 'path.to.view'.
{return} string fully resolved view in the format of 'path/to/view'.
源码: framework/web/actions/CViewAction.php#98 (显示)
protected function resolveView($viewPath)
{
    
// start with a word char and have word chars, dots and dashes only
    
if(preg_match('/^\w[\w\.\-]*$/',$viewPath))
    {
        
$view=strtr($viewPath,'.','/');
        if(!empty(
$this->basePath))
            
$view=$this->basePath.'/'.$view;
        if(
$this->getController()->getViewFile($view)!==false)
        {
            
$this->view=$view;
            return;
        }
    }
    throw new 
CHttpException(404,Yii::t('yii','The requested view "{name}" was not found.',
        array(
'{name}'=>$viewPath)));
}

Resolves the user-specified view into a valid view name.

run() 方法
public void run()
源码: framework/web/actions/CViewAction.php#121 (显示)
public function run()
{
    
$this->resolveView($this->getRequestedView());
    
$controller=$this->getController();
    if(
$this->layout!==null)
    {
        
$layout=$controller->layout;
        
$controller->layout=$this->layout;
    }

    
$this->onBeforeRender($event=new CEvent($this));
    if(!
$event->handled)
    {
        if(
$this->renderAsText)
        {
            
$text=file_get_contents($controller->getViewFile($this->view));
            
$controller->renderText($text);
        }
        else
            
$controller->render($this->view);
        
$this->onAfterRender(new CEvent($this));
    }

    if(
$this->layout!==null)
        
$controller->layout=$layout;
}

Runs the action. This method displays the view requested by the user.