Browse Source

Fix #18083: Add `Controller::$request` and `$response`

tags/2.0.36
Brandon Kelly 4 years ago committed by GitHub
parent
commit
fc4f449e21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 22
      framework/base/Controller.php
  3. 2
      framework/console/Controller.php
  4. 26
      framework/web/Controller.php
  5. 10
      tests/framework/console/controllers/CacheControllerTest.php
  6. 3
      tests/framework/web/ControllerTest.php

1
framework/CHANGELOG.md

@ -15,6 +15,7 @@ Yii Framework 2 Change Log
- Bug #13828: Fix retrieving inserted data for a primary key of type uniqueidentifier for SQL Server 2005 or later (darkdef)
- Bug #17474: Fix retrieving inserted data for a primary key of type trigger for SQL Server 2005 or later (darkdef)
- Bug #18001: Fix getting table metadata for tables `(` in their name (floor12)
- Enh #18083: Add `Controller::$request` and `$response` (brandonkelly)
- Enh #18102: Use “primary”/“replica” terminology instead of “master”/“slave” (brandonkelly)
- Added `yii\db\Connection::$enableReplicas` and deprecated `$enableSlaves` via magic methods.
- Added `yii\db\Connection::$replicas` and deprecated `$slaves` via magic methods.

22
framework/base/Controller.php

@ -8,6 +8,7 @@
namespace yii\base;
use Yii;
use yii\di\Instance;
/**
* Controller is the base class for classes containing controller logic.
@ -63,6 +64,16 @@ class Controller extends Component implements ViewContextInterface
* by [[run()]] when it is called by [[Application]] to run an action.
*/
public $action;
/**
* @var Request|array|string The request
* @since 2.0.36
*/
public $request = 'request';
/**
* @var Response|array|string
* @since 2.0.36
*/
public $response = 'response';
/**
* @var View the view object that can be used to render views or view files.
@ -87,6 +98,17 @@ class Controller extends Component implements ViewContextInterface
}
/**
* {@inheritdoc}
* @since 2.0.36
*/
public function init()
{
parent::init();
$this->request = Instance::ensure($this->request, Request::className());
$this->response = Instance::ensure($this->response, Response::className());
}
/**
* Declares external actions for the controller.
*
* This method is meant to be overwritten to declare external actions for the controller.

2
framework/console/Controller.php

@ -34,6 +34,8 @@ use yii\helpers\Inflector;
* read-only.
* @property array $passedOptions The names of the options passed during execution. This property is
* read-only.
* @property Request $request
* @property Response $response
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0

26
framework/web/Controller.php

@ -18,6 +18,8 @@ use yii\helpers\Url;
*
* 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>
* @since 2.0
*/
@ -72,10 +74,9 @@ class Controller extends \yii\base\Controller
*/
public function asJson($data)
{
$response = Yii::$app->getResponse();
$response->format = Response::FORMAT_JSON;
$response->data = $data;
return $response;
$this->response->format = Response::FORMAT_JSON;
$this->response->data = $data;
return $this->response;
}
/**
@ -99,10 +100,9 @@ class Controller extends \yii\base\Controller
*/
public function asXml($data)
{
$response = Yii::$app->getResponse();
$response->format = Response::FORMAT_XML;
$response->data = $data;
return $response;
$this->response->format = Response::FORMAT_XML;
$this->response->data = $data;
return $this->response;
}
/**
@ -200,7 +200,7 @@ class Controller extends \yii\base\Controller
public function beforeAction($action)
{
if (parent::beforeAction($action)) {
if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !Yii::$app->getRequest()->validateCsrfToken()) {
if ($this->enableCsrfValidation && Yii::$app->getErrorHandler()->exception === null && !$this->request->validateCsrfToken()) {
throw new BadRequestHttpException(Yii::t('yii', 'Unable to verify your data submission.'));
}
@ -239,7 +239,7 @@ class Controller extends \yii\base\Controller
public function redirect($url, $statusCode = 302)
{
// calling Url::to() here because Response::redirect() modifies route before calling Url::to()
return Yii::$app->getResponse()->redirect(Url::to($url), $statusCode);
return $this->response->redirect(Url::to($url), $statusCode);
}
/**
@ -256,7 +256,7 @@ class Controller extends \yii\base\Controller
*/
public function goHome()
{
return Yii::$app->getResponse()->redirect(Yii::$app->getHomeUrl());
return $this->response->redirect(Yii::$app->getHomeUrl());
}
/**
@ -279,7 +279,7 @@ class Controller extends \yii\base\Controller
*/
public function goBack($defaultUrl = null)
{
return Yii::$app->getResponse()->redirect(Yii::$app->getUser()->getReturnUrl($defaultUrl));
return $this->response->redirect(Yii::$app->getUser()->getReturnUrl($defaultUrl));
}
/**
@ -299,6 +299,6 @@ class Controller extends \yii\base\Controller
*/
public function refresh($anchor = '')
{
return Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->getUrl() . $anchor);
return $this->response->redirect($this->request->getUrl() . $anchor);
}
}

10
tests/framework/console/controllers/CacheControllerTest.php

@ -33,11 +33,6 @@ class CacheControllerTest extends TestCase
{
parent::setUp();
$this->_cacheController = Yii::createObject([
'class' => 'yiiunit\framework\console\controllers\SilencedCacheController',
'interactive' => false,
], [null, null]); //id and module are null
$databases = self::getParam('databases');
$config = $databases[$this->driverName];
$pdoDriver = 'pdo_' . $this->driverName;
@ -73,6 +68,11 @@ class CacheControllerTest extends TestCase
],
]);
$this->_cacheController = Yii::createObject([
'class' => 'yiiunit\framework\console\controllers\SilencedCacheController',
'interactive' => false,
], [null, null]); //id and module are null
if (isset($config['fixture'])) {
Yii::$app->db->open();
$lines = explode(';', file_get_contents($config['fixture']));

3
tests/framework/web/ControllerTest.php

@ -246,6 +246,7 @@ class ControllerTest extends TestCase
protected function setUp()
{
parent::setUp();
$this->mockWebApplication();
$this->controller = new FakeController('fake', new \yii\web\Application([
'id' => 'app',
'basePath' => __DIR__,
@ -258,6 +259,6 @@ class ControllerTest extends TestCase
],
],
]));
$this->mockWebApplication(['controller' => $this->controller]);
Yii::$app->controller = $this->controller;
}
}

Loading…
Cancel
Save