Skip to content
PHP Ecosystem
Explore

ViewAction

API reference › yii › web › ViewAction

ViewAction 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.

Users specify a view in the format of path/to/view, which translates to the view name ViewPrefix/path/to/view where ViewPrefix is given by $viewPrefix. The view will then be rendered by the render() method of the currently active controller.

Note that the user-specified view name must start with a word character and can only contain word characters, forward slashes, dots and dashes.

Not standalone-compatible: this action requires a hosting controller because rendering and layout resolution are performed through the controller. Register it via Controller::actions() rather than Module::$actionMap.

PropertyTypeDescriptionDefined by
$behaviors read-onlyarray<yii\base\Behavior<static>>List of behaviors attached to this component.yii\base\Component
$controlleryii\base\T|nullThe controller that owns this action, or null when the action runs standalone. via Module::$actionMap Default: null.yii\base\Action
$defaultViewstringThe name of the default view when ViewAction::$viewParam GET parameter is not provided by user. Default: 'index'.ViewAction
$idstring|nullID of the action; null between construction and dispatcher post-assignment for standalone actions resolved through Module::$actionMap or Module::$actionNamespace. Default: null.yii\base\Action
$layoutmixedThe name of the layout to be applied to the requested view. Default: null.ViewAction
$moduleyii\base\Module|nullThe owning module, or null if neither a module nor a controller is attached. Available since 22.0.yii\base\Action
$uniqueId read-onlystringThe unique ID of this action among the whole application.yii\base\Action
$viewParamstringThe name of the GET parameter that contains the requested view name. Default: 'view'.ViewAction
$viewPrefixstringA string to be prefixed to the user-specified view name to form a complete view name. Default: 'pages'.ViewAction
MethodDescriptionDefined by
__construct()Constructor.yii\base\Action
__call()Calls the named method which is not a class method.yii\base\Component
__clone()This method is called after the object is created by cloning an existing one.yii\base\Component
__get()Returns the value of a component property.yii\base\Component
__isset()Checks if a property is set, i.e. defined and not null.yii\base\Component
__set()Sets the value of a component property.yii\base\Component
__unset()Sets a component property to be null.yii\base\Component
attachBehavior()Attaches a behavior to this component.yii\base\Component
attachBehaviors()Attaches a list of behaviors to the component.yii\base\Component
behaviors()Returns a list of behaviors that this component should behave as.yii\base\Component
canGetProperty()Returns a value indicating whether a property can be read.yii\base\Component
canSetProperty()Returns a value indicating whether a property can be set.yii\base\Component
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
detachBehavior()Detaches a behavior from the component.yii\base\Component
detachBehaviors()Detaches all behaviors from the component.yii\base\Component
ensureBehaviors()Makes sure that the behaviors declared in behaviors() are attached to this component.yii\base\Component
getBehavior()Returns the named behavior object.yii\base\Component
getBehaviors()Returns all behaviors attached to this component.yii\base\Component
getModule()Returns the module that owns this action when invoked standalone.yii\base\Action
getUniqueId()Returns the unique ID of this action among the whole application.yii\base\Action
hasEventHandlers()Returns a value indicating whether there is any handler attached to the named event.yii\base\Component
hasMethod()Returns a value indicating whether a method is defined.yii\base\Component
hasProperty()Returns a value indicating whether a property is defined for this component.yii\base\Component
init()Initializes the object.yii\base\BaseObject
off()Detaches an existing event handler from this component.yii\base\Component
on()Attaches an event handler to an event.yii\base\Component
run()Runs the action.ViewAction
runWithParams()Runs this action with the specified parameters.yii\base\Action
setModule()Sets the module that owns this action when invoked standalone.yii\base\Action
trigger()Triggers an event.yii\base\Component
afterRun() protectedThis method is called right after run() is executed.yii\base\Action
beforeRun() protectedThis method is called right before run() is executed.yii\base\Action
render() protectedRenders a view.ViewAction
resolveStandaloneParams() protectedResolves typed parameters of run() when the action is invoked without a hosting controller.yii\base\Action
resolveViewName() protectedResolves the view name currently being requested.ViewAction
public string $defaultView = 'index'

The name of the default view when ViewAction::$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.

public mixed $layout = null

The name of the layout to be applied to the requested view.

This will be assigned to Controller::$layout before the view is rendered. Defaults to null, meaning the controller’s layout will be used. If false, no layout will be applied.

public string $viewPrefix = 'pages'

A string to be prefixed to the user-specified view name to form a complete view name.

For example, if a user requests for tutorial/chap1, the corresponding view name will be pages/tutorial/chap1, assuming the prefix is pages. The actual view file is determined by View::findViewFile().

public function run(): mixed

Runs the action.

This method displays the view requested by the user.

Returns

mixed

Throws

protected function render(string $viewName): string

Renders a view.

Parameters

ParameterTypeDescription
$viewNamestringView name

Returns

string: Result of the rendering

protected function resolveViewName(): string

Resolves the view name currently being requested.

Returns

string: The resolved view name

Throws