Skip to content

Commit

Permalink
Fix #18083: Add Controller::$request and $response
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Jun 14, 2020
1 parent d317e41 commit fc4f449
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions framework/base/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace yii\base;

use Yii;
use yii\di\Instance;

/**
* Controller is the base class for classes containing controller logic.
Expand Down Expand Up @@ -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.
Expand All @@ -86,6 +97,17 @@ public function __construct($id, $module, $config = [])
parent::__construct($config);
}

/**
* {@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.
*
Expand Down
2 changes: 2 additions & 0 deletions framework/console/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* 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
Expand Down
26 changes: 13 additions & 13 deletions framework/web/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*
* 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
*/
Expand Down Expand Up @@ -72,10 +74,9 @@ public function renderAjax($view, $params = [])
*/
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;
}

/**
Expand All @@ -99,10 +100,9 @@ public function asJson($data)
*/
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;
}

/**
Expand Down Expand Up @@ -200,7 +200,7 @@ public function bindActionParams($action, $params)
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.'));
}

Expand Down Expand Up @@ -239,7 +239,7 @@ public function beforeAction($action)
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);
}

/**
Expand All @@ -256,7 +256,7 @@ public function redirect($url, $statusCode = 302)
*/
public function goHome()
{
return Yii::$app->getResponse()->redirect(Yii::$app->getHomeUrl());
return $this->response->redirect(Yii::$app->getHomeUrl());
}

/**
Expand All @@ -279,7 +279,7 @@ public function goHome()
*/
public function goBack($defaultUrl = null)
{
return Yii::$app->getResponse()->redirect(Yii::$app->getUser()->getReturnUrl($defaultUrl));
return $this->response->redirect(Yii::$app->getUser()->getReturnUrl($defaultUrl));
}

/**
Expand All @@ -299,6 +299,6 @@ public function goBack($defaultUrl = null)
*/
public function refresh($anchor = '')
{
return Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->getUrl() . $anchor);
return $this->response->redirect($this->request->getUrl() . $anchor);
}
}
10 changes: 5 additions & 5 deletions tests/framework/console/controllers/CacheControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ protected function setUp()
{
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;
Expand Down Expand Up @@ -73,6 +68,11 @@ protected function setUp()
],
]);

$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']));
Expand Down
3 changes: 2 additions & 1 deletion tests/framework/web/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ public function testRedirect()
protected function setUp()
{
parent::setUp();
$this->mockWebApplication();
$this->controller = new FakeController('fake', new \yii\web\Application([
'id' => 'app',
'basePath' => __DIR__,
Expand All @@ -258,6 +259,6 @@ protected function setUp()
],
],
]));
$this->mockWebApplication(['controller' => $this->controller]);
Yii::$app->controller = $this->controller;
}
}

1 comment on commit fc4f449

@azzi907
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yii::$app->getResponse() has been changed to
$this->response
which causing issue
$this->response->redirect is required an array of route but even when providing array its still cause error saying a string is given

Please sign in to comment.