mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-10-31 10:39:59 +08:00 
			
		
		
		
	Fix #18083: Add Controller::$request and $response
				
					
				
			This commit is contained in:
		| @ -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 #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 #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) | - 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) | - 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::$enableReplicas` and deprecated `$enableSlaves` via magic methods. | ||||||
|   - Added `yii\db\Connection::$replicas` and deprecated `$slaves` via magic methods. |   - Added `yii\db\Connection::$replicas` and deprecated `$slaves` via magic methods. | ||||||
|  | |||||||
| @ -8,6 +8,7 @@ | |||||||
| namespace yii\base; | namespace yii\base; | ||||||
|  |  | ||||||
| use Yii; | use Yii; | ||||||
|  | use yii\di\Instance; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Controller is the base class for classes containing controller logic. |  * 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. |      * by [[run()]] when it is called by [[Application]] to run an action. | ||||||
|      */ |      */ | ||||||
|     public $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. |      * @var View the view object that can be used to render views or view files. | ||||||
| @ -86,6 +97,17 @@ class Controller extends Component implements ViewContextInterface | |||||||
|         parent::__construct($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. |      * Declares external actions for the controller. | ||||||
|      * |      * | ||||||
|  | |||||||
| @ -34,6 +34,8 @@ use yii\helpers\Inflector; | |||||||
|  * read-only. |  * read-only. | ||||||
|  * @property array $passedOptions The names of the options passed during execution. This property is |  * @property array $passedOptions The names of the options passed during execution. This property is | ||||||
|  * read-only. |  * read-only. | ||||||
|  |  * @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 | ||||||
|  | |||||||
| @ -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). |  * 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 | ||||||
|  */ |  */ | ||||||
| @ -72,10 +74,9 @@ class Controller extends \yii\base\Controller | |||||||
|      */ |      */ | ||||||
|     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; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @ -99,10 +100,9 @@ class Controller extends \yii\base\Controller | |||||||
|      */ |      */ | ||||||
|     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; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @ -200,7 +200,7 @@ class Controller extends \yii\base\Controller | |||||||
|     public function beforeAction($action) |     public function beforeAction($action) | ||||||
|     { |     { | ||||||
|         if (parent::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.')); |                 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) |     public function redirect($url, $statusCode = 302) | ||||||
|     { |     { | ||||||
|         // calling Url::to() here because Response::redirect() modifies route before calling Url::to() |         // 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() |     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) |     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 = '') |     public function refresh($anchor = '') | ||||||
|     { |     { | ||||||
|         return Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->getUrl() . $anchor); |         return $this->response->redirect($this->request->getUrl() . $anchor); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -33,11 +33,6 @@ class CacheControllerTest extends TestCase | |||||||
|     { |     { | ||||||
|         parent::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'); |         $databases = self::getParam('databases'); | ||||||
|         $config = $databases[$this->driverName]; |         $config = $databases[$this->driverName]; | ||||||
|         $pdoDriver = 'pdo_' . $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'])) { |         if (isset($config['fixture'])) { | ||||||
|             Yii::$app->db->open(); |             Yii::$app->db->open(); | ||||||
|             $lines = explode(';', file_get_contents($config['fixture'])); |             $lines = explode(';', file_get_contents($config['fixture'])); | ||||||
|  | |||||||
| @ -246,6 +246,7 @@ class ControllerTest extends TestCase | |||||||
|     protected function setUp() |     protected function setUp() | ||||||
|     { |     { | ||||||
|         parent::setUp(); |         parent::setUp(); | ||||||
|  |         $this->mockWebApplication(); | ||||||
|         $this->controller = new FakeController('fake', new \yii\web\Application([ |         $this->controller = new FakeController('fake', new \yii\web\Application([ | ||||||
|             'id' => 'app', |             'id' => 'app', | ||||||
|             'basePath' => __DIR__, |             'basePath' => __DIR__, | ||||||
| @ -258,6 +259,6 @@ class ControllerTest extends TestCase | |||||||
|                 ], |                 ], | ||||||
|             ], |             ], | ||||||
|         ])); |         ])); | ||||||
|         $this->mockWebApplication(['controller' => $this->controller]); |         Yii::$app->controller = $this->controller; | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Brandon Kelly
					Brandon Kelly