Commit 9cf82b3a by Qiang Xue

Added Model::load(). Added Controller::refresh() and redirect().

parent 71a156eb
......@@ -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'));
}
}
......@@ -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,
......
......@@ -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,
......
......@@ -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'];
......
......@@ -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
......
......@@ -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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment