0 follower

CViewAction

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

Hide inherited properties

PropertyTypeDescriptionDefined 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

Hide inherited methods

MethodDescriptionDefined 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
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

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
resolveView() Resolves the user-specified view into a valid view name. CViewAction

Events

Hide inherited events

EventDescriptionDefined 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#78 (show)
public function getRequestedView()
{
    if(
$this->_viewPath===null)
    {
        if(!empty(
$_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#161 (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#152 (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#96 (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}" is 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#119 (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.