PHP Class yii\web\Controller

Since: 2.0
Author: Qiang Xue ([email protected])
Inheritance: extends yii\base\Controller
ファイルを表示 Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$actionParams the parameters bound to the current action.
$enableCsrfValidation whether to enable CSRF validation for the actions in this controller. CSRF validation is enabled only when both this property and [[\yii\web\Request::enableCsrfValidation]] are true.

Public Methods

Method Description
asJson ( mixed $data ) : Response Send data formatted as JSON.
asXml ( mixed $data ) : Response Send data formatted as XML.
beforeAction ( $action )
bindActionParams ( Action $action, array $params ) : array Binds the parameters to the action.
goBack ( string | array $defaultUrl = null ) : Response Redirects the browser to the last visited page.
goHome ( ) : Response Redirects the browser to the home page.
redirect ( string | array $url, integer $statusCode = 302 ) : Response Redirects the browser to the specified URL.
refresh ( string $anchor = '' ) : Response Refreshes the current page.
renderAjax ( string $view, array $params = [] ) : string Renders a view in response to an AJAX request.

Method Details

asJson() public method

This method is a shortcut for sending data formatted as JSON. It will return the [[Application::getResponse()|response]] application component after configuring the [[Response::$format|format]] and setting the [[Response::$data|data]] that should be formatted. A common usage will be: php return $this->asJson($data);
See also: Response::$format
See also: Response::FORMAT_JSON
See also: JsonResponseFormatter
Since: 2.0.11
public asJson ( mixed $data ) : Response
$data mixed the data that should be formatted.
return Response a response that is configured to send `$data` formatted as JSON.

asXml() public method

This method is a shortcut for sending data formatted as XML. It will return the [[Application::getResponse()|response]] application component after configuring the [[Response::$format|format]] and setting the [[Response::$data|data]] that should be formatted. A common usage will be: php return $this->asXml($data);
See also: Response::$format
See also: Response::FORMAT_XML
See also: XmlResponseFormatter
Since: 2.0.11
public asXml ( mixed $data ) : Response
$data mixed the data that should be formatted.
return Response a response that is configured to send `$data` formatted as XML.

beforeAction() public method

public beforeAction ( $action )

bindActionParams() public method

This method is invoked by Action when it begins to run with the given parameters. This method will check the parameter names that the action requires and return the provided parameters according to the requirement. If there is any missing parameter, an exception will be thrown.
public bindActionParams ( Action $action, array $params ) : array
$action yii\base\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.

goBack() public method

You can use this method in an action by returning the Response directly: php stop executing this action and redirect to last visited page return $this->goBack(); For this function to work you have to [[User::setReturnUrl()|set the return URL]] in appropriate places before.
See also: User::getReturnUrl()
public goBack ( string | array $defaultUrl = null ) : Response
$defaultUrl string | array the default return URL in case it was not set previously. If this is null and the return URL was not set previously, [[Application::homeUrl]] will be redirected to. Please refer to [[User::setReturnUrl()]] on accepted format of the URL.
return Response the current response object

goHome() public method

You can use this method in an action by returning the Response directly: php stop executing this action and redirect to home page return $this->goHome();
public goHome ( ) : Response
return Response the current response object

redirect() public method

This method is a shortcut to [[Response::redirect()]]. You can use it in an action by returning the Response directly: php stop executing this action and redirect to login page return $this->redirect(['login']);
public redirect ( string | array $url, integer $statusCode = 302 ) : Response
$url string | array the URL to be redirected to. This can be in one of the following formats: - a string representing a URL (e.g. "http://example.com") - a string representing a URL alias (e.g. "@example.com") - an array in the format of `[$route, ...name-value pairs...]` (e.g. `['site/index', 'ref' => 1]`) [[Url::to()]] will be used to convert the array into a URL. Any relative URL will be converted into an absolute one by prepending it with the host info of the current request.
$statusCode integer the HTTP status code. Defaults to 302. See for details about HTTP status code
return Response the current response object

refresh() public method

This method is a shortcut to [[Response::refresh()]]. You can use it in an action by returning the Response directly: php stop executing this action and refresh the current page return $this->refresh();
public refresh ( string $anchor = '' ) : Response
$anchor string the anchor that should be appended to the redirection URL. Defaults to empty. Make sure the anchor starts with '#' if you want to specify it.
return Response the response object itself

renderAjax() public method

This method is similar to [[renderPartial()]] except that it will inject into the rendering result with JS/CSS scripts and files which are registered with the view. For this reason, you should use this method instead of [[renderPartial()]] to render a view to respond to an AJAX request.
public renderAjax ( 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.

Property Details

$actionParams public_oe property

the parameters bound to the current action.
public $actionParams

$enableCsrfValidation public_oe property

whether to enable CSRF validation for the actions in this controller. CSRF validation is enabled only when both this property and [[\yii\web\Request::enableCsrfValidation]] are true.
public $enableCsrfValidation