CViewAction

Package system.web.actions
Inheritance class CViewAction » CAction » CComponent
Implements IAction
Since 1.0
Source Code 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.

Public Properties

Property Type Description Defined By
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

Public Methods

Method Description Defined By
__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

Protected Methods

Method Description Defined By
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

Events

Event Description Defined By
onBeforeRender Raised right before the action invokes the render method. CViewAction
onAfterRender Raised right after the action invokes the render method. CViewAction

Property Details

basePath property

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 property

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.

See Also

layout property

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 property

public boolean $renderAsText;

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

requestedView property read-only

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 property

public string $view;

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

viewParam property

public string $viewParam;

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

Method Details

getRequestedView() method

public string getRequestedView()
{return} string the name of the view requested by the user. This is in the format of 'path.to.view'.
Source Code: framework/web/actions/CViewAction.php#80 (show)
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() method

public void onAfterRender(CEvent $event)
$event CEvent event parameter
Source Code: framework/web/actions/CViewAction.php#163 (show)
public function onAfterRender($event)
{
    
$this->raiseEvent('onAfterRender',$event);
}

Raised right after the action invokes the render method.

onBeforeRender() method

public void onBeforeRender(CEvent $event)
$event CEvent event parameter
Source Code: framework/web/actions/CViewAction.php#154 (show)
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() method

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'.
Source Code: framework/web/actions/CViewAction.php#98 (show)
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() method

public void run()
Source Code: framework/web/actions/CViewAction.php#121 (show)
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.

© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc/api/1.1/CViewAction