Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controller::$request and $response #18083

Merged
merged 6 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)


2.0.35 May 02, 2020
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
samdark marked this conversation as resolved.
Show resolved Hide resolved
* @since 2.0.36
*/
public $request = 'request';
samdark marked this conversation as resolved.
Show resolved Hide resolved
/**
* @var Response|array|string
samdark marked this conversation as resolved.
Show resolved Hide resolved
* @since 2.0.36
*/
public $response = 'response';
samdark marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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;
}
}