From 9cf82b3ae1d64aaefbe2a7e45da1ca7a23e9e78a Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 26 Jun 2013 08:34:27 -0400 Subject: [PATCH] Added Model::load(). Added Controller::refresh() and redirect(). --- .../backend/controllers/SiteController.php | 6 ++--- .../frontend/controllers/SiteController.php | 10 ++++---- apps/basic/controllers/SiteController.php | 10 ++++---- docs/guide/upgrade-from-v1.md | 2 +- framework/yii/base/Model.php | 22 ++++++++++++++++ framework/yii/web/Controller.php | 30 ++++++++++++++++++++++ 6 files changed, 66 insertions(+), 14 deletions(-) diff --git a/apps/advanced/backend/controllers/SiteController.php b/apps/advanced/backend/controllers/SiteController.php index 851fcec..09052d2 100644 --- a/apps/advanced/backend/controllers/SiteController.php +++ b/apps/advanced/backend/controllers/SiteController.php @@ -16,8 +16,8 @@ class SiteController extends Controller public function actionLogin() { $model = new LoginForm(); - if ($this->populate($_POST, $model) && $model->login()) { - return Yii::$app->response->redirect(array('site/index')); + if ($model->load($_POST) && $model->login()) { + return $this->redirect(array('site/index')); } else { return $this->render('login', array( 'model' => $model, @@ -28,6 +28,6 @@ class SiteController extends Controller public function actionLogout() { Yii::$app->user->logout(); - return Yii::$app->response->redirect(array('site/index')); + return $this->redirect(array('site/index')); } } diff --git a/apps/advanced/frontend/controllers/SiteController.php b/apps/advanced/frontend/controllers/SiteController.php index b0f8ec2..85304d6 100644 --- a/apps/advanced/frontend/controllers/SiteController.php +++ b/apps/advanced/frontend/controllers/SiteController.php @@ -26,8 +26,8 @@ class SiteController extends Controller public function actionLogin() { $model = new LoginForm(); - if ($this->populate($_POST, $model) && $model->login()) { - return Yii::$app->response->redirect(array('site/index')); + if ($model->load($_POST) && $model->login()) { + return $this->redirect(array('site/index')); } else { return $this->render('login', array( 'model' => $model, @@ -38,15 +38,15 @@ class SiteController extends Controller public function actionLogout() { Yii::$app->user->logout(); - return Yii::$app->response->redirect(array('site/index')); + return $this->redirect(array('site/index')); } public function actionContact() { $model = new ContactForm; - if ($this->populate($_POST, $model) && $model->contact(Yii::$app->params['adminEmail'])) { + if ($model->load($_POST) && $model->contact(Yii::$app->params['adminEmail'])) { Yii::$app->session->setFlash('contactFormSubmitted'); - return Yii::$app->response->refresh(); + return $this->refresh(); } else { return $this->render('contact', array( 'model' => $model, diff --git a/apps/basic/controllers/SiteController.php b/apps/basic/controllers/SiteController.php index 3a6ef5c..9df4819 100644 --- a/apps/basic/controllers/SiteController.php +++ b/apps/basic/controllers/SiteController.php @@ -27,8 +27,8 @@ class SiteController extends Controller public function actionLogin() { $model = new LoginForm(); - if ($this->populate($_POST, $model) && $model->login()) { - return Yii::$app->response->redirect(array('site/index')); + if ($model->load($_POST) && $model->login()) { + return $this->redirect(array('site/index')); } else { return $this->render('login', array( 'model' => $model, @@ -39,15 +39,15 @@ class SiteController extends Controller public function actionLogout() { Yii::$app->user->logout(); - return Yii::$app->response->redirect(array('site/index')); + return $this->redirect(array('site/index')); } public function actionContact() { $model = new ContactForm; - if ($this->populate($_POST, $model) && $model->contact(Yii::$app->params['adminEmail'])) { + if ($model->load($_POST) && $model->contact(Yii::$app->params['adminEmail'])) { Yii::$app->session->setFlash('contactFormSubmitted'); - return Yii::$app->response->refresh(); + return $this->refresh(); } else { return $this->render('contact', array( 'model' => $model, diff --git a/docs/guide/upgrade-from-v1.md b/docs/guide/upgrade-from-v1.md index 66bce10..5f1cf72 100644 --- a/docs/guide/upgrade-from-v1.md +++ b/docs/guide/upgrade-from-v1.md @@ -201,7 +201,7 @@ to a model. For example, ```php $model = new Post; -if ($this->populate($_POST, $model)) {...} +if ($model->load($_POST)) {...} // which is equivalent to: if (isset($_POST['Post'])) { $model->attributes = $_POST['Post']; diff --git a/framework/yii/base/Model.php b/framework/yii/base/Model.php index ae739fc..dadf76c 100644 --- a/framework/yii/base/Model.php +++ b/framework/yii/base/Model.php @@ -638,6 +638,28 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess } /** + * Populates the model with the data from end user. + * The data is subject to the safety check by [[setAttributes()]]. If [[formName()]] is not empty, + * the data indexed by [[formName()]] in `$data` will be used to populate the model. + * @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array + * supplied by end user. + * @return boolean whether the model is successfully populated with some data. + */ + public function load($data) + { + $scope = $this->formName(); + if ($scope == '') { + $this->setAttributes($data); + return true; + } elseif (isset($data[$scope])) { + $this->setAttributes($data[$scope]); + return true; + } else { + return false; + } + } + + /** * Converts the object into an array. * The default implementation will return [[attributes]]. * @return array the array representation of the object diff --git a/framework/yii/web/Controller.php b/framework/yii/web/Controller.php index 6214c54..5152c11 100644 --- a/framework/yii/web/Controller.php +++ b/framework/yii/web/Controller.php @@ -83,4 +83,34 @@ class Controller extends \yii\base\Controller } return Yii::$app->getUrlManager()->createUrl($route, $params); } + + /** + * Redirects the browser to the specified URL. + * This method is a shortcut to [[Response::redirect()]]. + * + * @param array|string $url the URL to be redirected to. [[\yii\helpers\Html::url()]] + * will be used to normalize the URL. If the resulting URL is still a relative URL + * (one without host info), the current request host info will be used. + * @param integer $statusCode the HTTP status code. If null, it will use 302 + * for normal requests, and [[ajaxRedirectCode]] for AJAX requests. + * See [[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]] + * for details about HTTP status code + * @return Response the response object itself + */ + public function redirect($url, $statusCode = null) + { + return Yii::$app->getResponse()->redirect($url, $statusCode); + } + + /** + * Refreshes the current page. + * This method is a shortcut to [[Response::refresh()]]. + * @param string $anchor the anchor that should be appended to the redirection URL. + * Defaults to empty. Make sure the anchor starts with '#' if you want to specify it. + * @return Response the response object itself + */ + public function refresh($anchor = '') + { + return Yii::$app->getResponse()->redirect(Yii::$app->getRequest()->getUrl() . $anchor); + } }