"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "framework/web/Controller.php" between
yii2-2.0.35.tar.gz and yii2-2.0.36.tar.gz

About: Yii 2 is a high-performance component-based PHP framework for developing large-scale Web applications (source).

Controller.php  (yii2-2.0.35):Controller.php  (yii2-2.0.36)
<?php <?php
/** /**
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC * @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
namespace yii\web; namespace yii\web;
use Yii; use Yii;
use yii\base\ErrorException;
use yii\base\Exception;
use yii\base\InlineAction; use yii\base\InlineAction;
use yii\helpers\Url; use yii\helpers\Url;
/** /**
* Controller is the base class of web controllers. * Controller is the base class of web controllers.
* *
* For more details and usage information on Controller, see the [guide article on controllers](guide:structure-controllers). * For more details and usage information on Controller, see the [guide article on controllers](guide:structure-controllers).
* *
* @property Request $request
* @property Response $response
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class Controller extends \yii\base\Controller class Controller extends \yii\base\Controller
{ {
/** /**
* @var bool whether to enable CSRF validation for the actions in this contr oller. * @var bool whether to enable CSRF validation for the actions in this contr oller.
* CSRF validation is enabled only when both this property and [[\yii\web\Re quest::enableCsrfValidation]] are true. * CSRF validation is enabled only when both this property and [[\yii\web\Re quest::enableCsrfValidation]] are true.
*/ */
public $enableCsrfValidation = true; public $enableCsrfValidation = true;
skipping to change at line 72 skipping to change at line 76
* *
* @param mixed $data the data that should be formatted. * @param mixed $data the data that should be formatted.
* @return Response a response that is configured to send `$data` formatted as JSON. * @return Response a response that is configured to send `$data` formatted as JSON.
* @since 2.0.11 * @since 2.0.11
* @see Response::$format * @see Response::$format
* @see Response::FORMAT_JSON * @see Response::FORMAT_JSON
* @see JsonResponseFormatter * @see JsonResponseFormatter
*/ */
public function asJson($data) public function asJson($data)
{ {
$response = Yii::$app->getResponse(); $this->response->format = Response::FORMAT_JSON;
$response->format = Response::FORMAT_JSON; $this->response->data = $data;
$response->data = $data; return $this->response;
return $response;
} }
/** /**
* Send data formatted as XML. * Send data formatted as XML.
* *
* This method is a shortcut for sending data formatted as XML. It will retu rn * This method is a shortcut for sending data formatted as XML. It will retu rn
* the [[Application::getResponse()|response]] application component after c onfiguring * the [[Application::getResponse()|response]] application component after c onfiguring
* the [[Response::$format|format]] and setting the [[Response::$data|data]] that should * the [[Response::$format|format]] and setting the [[Response::$data|data]] that should
* be formatted. A common usage will be: * be formatted. A common usage will be:
* *
skipping to change at line 99 skipping to change at line 102
* *
* @param mixed $data the data that should be formatted. * @param mixed $data the data that should be formatted.
* @return Response a response that is configured to send `$data` formatted as XML. * @return Response a response that is configured to send `$data` formatted as XML.
* @since 2.0.11 * @since 2.0.11
* @see Response::$format * @see Response::$format
* @see Response::FORMAT_XML * @see Response::FORMAT_XML
* @see XmlResponseFormatter * @see XmlResponseFormatter
*/ */
public function asXml($data) public function asXml($data)
{ {
$response = Yii::$app->getResponse(); $this->response->format = Response::FORMAT_XML;
$response->format = Response::FORMAT_XML; $this->response->data = $data;
$response->data = $data; return $this->response;
return $response;
} }
/** /**
* Binds the parameters to the action. * Binds the parameters to the action.
* This method is invoked by [[\yii\base\Action]] when it begins to run with the given parameters. * This method is invoked by [[\yii\base\Action]] when it begins to run with the given parameters.
* This method will check the parameter names that the action requires and r eturn * This method will check the parameter names that the action requires and r eturn
* the provided parameters according to the requirement. If there is any mis sing parameter, * the provided parameters according to the requirement. If there is any mis sing parameter,
* an exception will be thrown. * an exception will be thrown.
* @param \yii\base\Action $action the action to be bound with parameters * @param \yii\base\Action $action the action to be bound with parameters
* @param array $params the parameters to be bound to the action * @param array $params the parameters to be bound to the action
skipping to change at line 127 skipping to change at line 129
{ {
if ($action instanceof InlineAction) { if ($action instanceof InlineAction) {
$method = new \ReflectionMethod($this, $action->actionMethod); $method = new \ReflectionMethod($this, $action->actionMethod);
} else { } else {
$method = new \ReflectionMethod($action, 'run'); $method = new \ReflectionMethod($action, 'run');
} }
$args = []; $args = [];
$missing = []; $missing = [];
$actionParams = []; $actionParams = [];
$requestedParams = [];
foreach ($method->getParameters() as $param) { foreach ($method->getParameters() as $param) {
$name = $param->getName(); $name = $param->getName();
if (array_key_exists($name, $params)) { if (array_key_exists($name, $params)) {
$isValid = true; $isValid = true;
if ($param->isArray()) { if ($param->isArray()) {
$params[$name] = (array)$params[$name]; $params[$name] = (array)$params[$name];
} elseif (is_array($params[$name])) { } elseif (is_array($params[$name])) {
$isValid = false; $isValid = false;
} elseif ( } elseif (
PHP_VERSION_ID >= 70000 && PHP_VERSION_ID >= 70000 &&
skipping to change at line 164 skipping to change at line 167
$isValid = false; $isValid = false;
} }
} }
if (!$isValid) { if (!$isValid) {
throw new BadRequestHttpException(Yii::t('yii', 'Invalid dat a received for parameter "{param}".', [ throw new BadRequestHttpException(Yii::t('yii', 'Invalid dat a received for parameter "{param}".', [
'param' => $name, 'param' => $name,
])); ]));
} }
$args[] = $actionParams[$name] = $params[$name]; $args[] = $actionParams[$name] = $params[$name];
unset($params[$name]); unset($params[$name]);
} elseif (PHP_VERSION_ID >= 70100 && ($type = $param->getType()) !==
null && !$type->isBuiltin()) {
try {
$this->bindInjectedParams($type, $name, $args, $requestedPar
ams);
} catch (Exception $e) {
throw new ServerErrorHttpException($e->getMessage(), 0, $e);
}
} elseif ($param->isDefaultValueAvailable()) { } elseif ($param->isDefaultValueAvailable()) {
$args[] = $actionParams[$name] = $param->getDefaultValue(); $args[] = $actionParams[$name] = $param->getDefaultValue();
} else { } else {
$missing[] = $name; $missing[] = $name;
} }
} }
if (!empty($missing)) { if (!empty($missing)) {
throw new BadRequestHttpException(Yii::t('yii', 'Missing required pa rameters: {params}', [ throw new BadRequestHttpException(Yii::t('yii', 'Missing required pa rameters: {params}', [
'params' => implode(', ', $missing), 'params' => implode(', ', $missing),
])); ]));
} }
$this->actionParams = $actionParams; $this->actionParams = $actionParams;
// We use a different array here, specifically one that doesn't contain
service instances but descriptions instead.
if (\Yii::$app->requestedParams === null) {
\Yii::$app->requestedParams = array_merge($actionParams, $requestedP
arams);
}
return $args; return $args;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function beforeAction($action) public function beforeAction($action)
{ {
if (parent::beforeAction($action)) { if (parent::beforeAction($action)) {
if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exc eption === null && !Yii::$app->getRequest()->validateCsrfToken()) { if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exc eption === null && !$this->request->validateCsrfToken()) {
throw new BadRequestHttpException(Yii::t('yii', 'Unable to verif y your data submission.')); throw new BadRequestHttpException(Yii::t('yii', 'Unable to verif y your data submission.'));
} }
return true; return true;
} }
return false; return false;
} }
/** /**
skipping to change at line 227 skipping to change at line 241
* into an absolute one by prepending it with the host info of the current r equest. * into an absolute one by prepending it with the host info of the current r equest.
* *
* @param int $statusCode the HTTP status code. Defaults to 302. * @param int $statusCode the HTTP status code. Defaults to 302.
* See <https://tools.ietf.org/html/rfc2616#section-10> * See <https://tools.ietf.org/html/rfc2616#section-10>
* for details about HTTP status code * for details about HTTP status code
* @return Response the current response object * @return Response the current response object
*/ */
public function redirect($url, $statusCode = 302) public function redirect($url, $statusCode = 302)
{ {
// calling Url::to() here because Response::redirect() modifies route be fore calling Url::to() // calling Url::to() here because Response::redirect() modifies route be fore calling Url::to()
return Yii::$app->getResponse()->redirect(Url::to($url), $statusCode); return $this->response->redirect(Url::to($url), $statusCode);
} }
/** /**
* Redirects the browser to the home page. * Redirects the browser to the home page.
* *
* You can use this method in an action by returning the [[Response]] direct ly: * You can use this method in an action by returning the [[Response]] direct ly:
* *
* ```php * ```php
* // stop executing this action and redirect to home page * // stop executing this action and redirect to home page
* return $this->goHome(); * return $this->goHome();
* ``` * ```
* *
* @return Response the current response object * @return Response the current response object
*/ */
public function goHome() public function goHome()
{ {
return Yii::$app->getResponse()->redirect(Yii::$app->getHomeUrl()); return $this->response->redirect(Yii::$app->getHomeUrl());
} }
/** /**
* Redirects the browser to the last visited page. * Redirects the browser to the last visited page.
* *
* You can use this method in an action by returning the [[Response]] direct ly: * You can use this method in an action by returning the [[Response]] direct ly:
* *
* ```php * ```php
* // stop executing this action and redirect to last visited page * // stop executing this action and redirect to last visited page
* return $this->goBack(); * return $this->goBack();
skipping to change at line 267 skipping to change at line 281
* For this function to work you have to [[User::setReturnUrl()|set the retu rn URL]] in appropriate places before. * For this function to work you have to [[User::setReturnUrl()|set the retu rn URL]] in appropriate places before.
* *
* @param string|array $defaultUrl the default return URL in case it was not set previously. * @param string|array $defaultUrl 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. * 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. * Please refer to [[User::setReturnUrl()]] on accepted format of the URL.
* @return Response the current response object * @return Response the current response object
* @see User::getReturnUrl() * @see User::getReturnUrl()
*/ */
public function goBack($defaultUrl = null) public function goBack($defaultUrl = null)
{ {
return Yii::$app->getResponse()->redirect(Yii::$app->getUser()->getRetur nUrl($defaultUrl)); return $this->response->redirect(Yii::$app->getUser()->getReturnUrl($def aultUrl));
} }
/** /**
* Refreshes the current page. * Refreshes the current page.
* This method is a shortcut to [[Response::refresh()]]. * This method is a shortcut to [[Response::refresh()]].
* *
* You can use it in an action by returning the [[Response]] directly: * You can use it in an action by returning the [[Response]] directly:
* *
* ```php * ```php
* // stop executing this action and refresh the current page * // stop executing this action and refresh the current page
* return $this->refresh(); * return $this->refresh();
* ``` * ```
* *
* @param string $anchor the anchor that should be appended to the redirecti on URL. * @param string $anchor the anchor that should be appended to the redirecti on URL.
* Defaults to empty. Make sure the anchor starts with '#' if you want to sp ecify it. * Defaults to empty. Make sure the anchor starts with '#' if you want to sp ecify it.
* @return Response the response object itself * @return Response the response object itself
*/ */
public function refresh($anchor = '') public function refresh($anchor = '')
{ {
return Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->getUr l() . $anchor); return $this->response->redirect($this->request->getUrl() . $anchor);
} }
} }
 End of changes. 12 change blocks. 
13 lines changed or deleted 31 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)