Browse Source

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

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
9cf82b3ae1
  1. 6
      apps/advanced/backend/controllers/SiteController.php
  2. 10
      apps/advanced/frontend/controllers/SiteController.php
  3. 10
      apps/basic/controllers/SiteController.php
  4. 2
      docs/guide/upgrade-from-v1.md
  5. 22
      framework/yii/base/Model.php
  6. 30
      framework/yii/web/Controller.php

6
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'));
}
}

10
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,

10
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,

2
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'];

22
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

30
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);
}
}

Loading…
Cancel
Save