PHP Class yii\base\Controller

Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends Component, implements yii\base\ViewContextInterface
Show file Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$action the action that is currently being executed. This property will be set by Controller::run when it is called by Application to run an action.
$defaultAction the ID of the action that is used when the action ID is not specified in the request. Defaults to 'index'.
$id the ID of this controller.
$layout the name of the layout to be applied to this controller's views. This property mainly affects the behavior of Controller::render. Defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value. If false, no layout will be applied.
$module Module the module that this controller belongs to.

Public Methods

Method Description
__construct ( string $id, Module $module, array $config = [] )
actions ( ) Declares external actions for the controller.
afterAction ( Action $action, mixed $result ) : mixed This method is invoked right after an action is executed.
beforeAction ( Action $action ) : boolean This method is invoked right before an action is executed.
bindActionParams ( Action $action, array $params ) : array Binds the parameters to the action.
createAction ( string $id ) : Action Creates an action based on the given action ID.
findLayoutFile ( View $view ) : string | boolean Finds the applicable layout file.
getModules ( ) : Module[] Returns all ancestor modules of this controller.
getRoute ( ) : string Returns the route of the current request.
getUniqueId ( ) : string Returns the unique ID of the controller.
getView ( ) : View | View Returns the view object that can be used to render views or view files.
getViewPath ( ) : string Returns the directory containing view files for this controller.
render ( string $view, array $params = [] ) : string Renders a view and applies layout if available.
renderContent ( string $content ) : string Renders a static string by applying a layout.
renderFile ( string $file, array $params = [] ) : string Renders a view file.
renderPartial ( string $view, array $params = [] ) : string Renders a view without applying layout.
run ( string $route, array $params = [] ) : mixed Runs a request specified in terms of a route.
runAction ( string $id, array $params = [] ) : mixed Runs an action within this controller with the specified action ID and parameters.
setView ( View | View $view ) Sets the view object to be used by this controller.
setViewPath ( string $path ) Sets the directory that contains the view files.

Method Details

__construct() public method

public __construct ( string $id, Module $module, array $config = [] )
$id string the ID of this controller.
$module Module the module that this controller belongs to.
$config array name-value pairs that will be used to initialize the object properties.

actions() public method

This method is meant to be overwritten to declare external actions for the controller. It should return an array, with array keys being action IDs, and array values the corresponding action class names or action configuration arrays. For example, php return [ 'action1' => 'app\components\Action1', 'action2' => [ 'class' => 'app\components\Action2', 'property1' => 'value1', 'property2' => 'value2', ], ]; [[\Yii::createObject()]] will be used later to create the requested action using the configuration provided here.
public actions ( )

afterAction() public method

The method will trigger the [[EVENT_AFTER_ACTION]] event. The return value of the method will be used as the action return value. If you override this method, your code should look like the following: php public function afterAction($action, $result) { $result = parent::afterAction($action, $result); your custom code here return $result; }
public afterAction ( Action $action, mixed $result ) : mixed
$action Action the action just executed.
$result mixed the action return result.
return mixed the processed action result.

beforeAction() public method

The method will trigger the [[EVENT_BEFORE_ACTION]] event. The return value of the method will determine whether the action should continue to run. In case the action should not run, the request should be handled inside of the beforeAction code by either providing the necessary output or redirecting the request. Otherwise the response will be empty. If you override this method, your code should look like the following: php public function beforeAction($action) { your custom code here, if you want the code to run before action filters, which are triggered on the [[EVENT_BEFORE_ACTION]] event, e.g. PageCache or AccessControl if (!parent::beforeAction($action)) { return false; } other custom code here return true; // or false to not run the action }
public beforeAction ( Action $action ) : boolean
$action Action the action to be executed.
return boolean whether the action should continue to run.

bindActionParams() public method

This method is invoked by Action when it begins to run with the given parameters.
public bindActionParams ( Action $action, array $params ) : array
$action Action the action to be bound with parameters.
$params array the parameters to be bound to the action.
return array the valid parameters that the action can run with.

createAction() public method

The method first checks if the action ID has been declared in Controller::actions. If so, it will use the configuration declared there to create the action object. If not, it will look for a controller method whose name is in the format of actionXyz where Xyz stands for the action ID. If found, an InlineAction representing that method will be created and returned.
public createAction ( string $id ) : Action
$id string the action ID.
return Action the newly created action instance. Null if the ID doesn't resolve into any action.

findLayoutFile() public method

Finds the applicable layout file.
public findLayoutFile ( View $view ) : string | boolean
$view View the view object to render the layout file.
return string | boolean the layout file path, or false if layout is not needed. Please refer to [[render()]] on how to specify this parameter.

getModules() public method

The first module in the array is the outermost one (i.e., the application instance), while the last is the innermost one.
public getModules ( ) : Module[]
return Module[] all ancestor modules that this controller is located within.

getRoute() public method

Returns the route of the current request.
public getRoute ( ) : string
return string the route (module ID, controller ID and action ID) of the current request.

getUniqueId() public method

Returns the unique ID of the controller.
public getUniqueId ( ) : string
return string the controller ID that is prefixed with the module ID (if any).

getView() public method

The Controller::render, Controller::renderPartial and Controller::renderFile methods will use this view object to implement the actual view rendering. If not set, it will default to the "view" application component.
public getView ( ) : View | View
return View | View the view object that can be used to render views or view files.

getViewPath() public method

The default implementation returns the directory named as controller [[id]] under the [[module]]'s [[viewPath]] directory.
public getViewPath ( ) : string
return string the directory containing the view files for this controller.

render() public method

The view to be rendered can be specified in one of the following formats: - path alias (e.g. "@app/views/site/index"); - absolute path within application (e.g. "//site/index"): the view name starts with double slashes. The actual view file will be looked for under the [[Application::viewPath|view path]] of the application. - absolute path within module (e.g. "/site/index"): the view name starts with a single slash. The actual view file will be looked for under the [[Module::viewPath|view path]] of [[module]]. - relative path (e.g. "index"): the actual view file will be looked for under [[viewPath]]. To determine which layout should be applied, the following two steps are conducted: 1. In the first step, it determines the layout name and the context module: - If [[layout]] is specified as a string, use it as the layout name and [[module]] as the context module; - If [[layout]] is null, search through all ancestor modules of this controller and find the first module whose [[Module::layout|layout]] is not null. The layout and the corresponding module are used as the layout name and the context module, respectively. If such a module is not found or the corresponding layout is not a string, it will return false, meaning no applicable layout. 2. In the second step, it determines the actual layout file according to the previously found layout name and context module. The layout name can be: - a path alias (e.g. "@app/views/layouts/main"); - an absolute path (e.g. "/main"): the layout name starts with a slash. The actual layout file will be looked for under the [[Application::layoutPath|layout path]] of the application; - a relative path (e.g. "main"): the actual layout file will be looked for under the [[Module::layoutPath|layout path]] of the context module. If the layout name does not contain a file extension, it will use the default one .php.
public render ( string $view, array $params = [] ) : string
$view string the view name.
$params array the parameters (name-value pairs) that should be made available in the view. These parameters will not be available in the layout.
return string the rendering result.

renderContent() public method

Renders a static string by applying a layout.
Since: 2.0.1
public renderContent ( string $content ) : string
$content string the static string being rendered
return string the rendering result of the layout with the given static string as the `$content` variable. If the layout is disabled, the string will be returned back.

renderFile() public method

Renders a view file.
public renderFile ( string $file, array $params = [] ) : string
$file string the view file to be rendered. This can be either a file path or a path alias.
$params array the parameters (name-value pairs) that should be made available in the view.
return string the rendering result.

renderPartial() public method

This method differs from Controller::render in that it does not apply any layout.
public renderPartial ( string $view, array $params = [] ) : string
$view string the view name. Please refer to [[render()]] on how to specify a view name.
$params array the parameters (name-value pairs) that should be made available in the view.
return string the rendering result.

run() public method

The route can be either an ID of an action within this controller or a complete route consisting of module IDs, controller ID and action ID. If the route starts with a slash '/', the parsing of the route will start from the application; otherwise, it will start from the parent module of this controller.
See also: runAction()
public run ( string $route, array $params = [] ) : mixed
$route string the route to be handled, e.g., 'view', 'comment/view', '/admin/comment/view'.
$params array the parameters to be passed to the action.
return mixed the result of the action.

runAction() public method

If the action ID is empty, the method will use [[defaultAction]].
See also: createAction()
public runAction ( string $id, array $params = [] ) : mixed
$id string the ID of the action to be executed.
$params array the parameters (name-value pairs) to be passed to the action.
return mixed the result of the action.

setView() public method

Sets the view object to be used by this controller.
public setView ( View | View $view )
$view View | View the view object that can be used to render views or view files.

setViewPath() public method

Sets the directory that contains the view files.
Since: 2.0.7
public setViewPath ( string $path )
$path string the root directory of view files.

Property Details

$action public property

the action that is currently being executed. This property will be set by Controller::run when it is called by Application to run an action.
public $action

$defaultAction public property

the ID of the action that is used when the action ID is not specified in the request. Defaults to 'index'.
public $defaultAction

$id public property

the ID of this controller.
public $id

$layout public property

the name of the layout to be applied to this controller's views. This property mainly affects the behavior of Controller::render. Defaults to null, meaning the actual layout value should inherit that from [[module]]'s layout value. If false, no layout will be applied.
public $layout

$module public property

the module that this controller belongs to.
public Module,yii\base $module
return Module