mirror of
https://github.com/yiisoft/yii2.git
synced 2025-12-02 08:00:50 +08:00
Added Model::load(). Added Controller::refresh() and redirect().
This commit is contained in:
@@ -16,8 +16,8 @@ class SiteController extends Controller
|
|||||||
public function actionLogin()
|
public function actionLogin()
|
||||||
{
|
{
|
||||||
$model = new LoginForm();
|
$model = new LoginForm();
|
||||||
if ($this->populate($_POST, $model) && $model->login()) {
|
if ($model->load($_POST) && $model->login()) {
|
||||||
return Yii::$app->response->redirect(array('site/index'));
|
return $this->redirect(array('site/index'));
|
||||||
} else {
|
} else {
|
||||||
return $this->render('login', array(
|
return $this->render('login', array(
|
||||||
'model' => $model,
|
'model' => $model,
|
||||||
@@ -28,6 +28,6 @@ class SiteController extends Controller
|
|||||||
public function actionLogout()
|
public function actionLogout()
|
||||||
{
|
{
|
||||||
Yii::$app->user->logout();
|
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()
|
public function actionLogin()
|
||||||
{
|
{
|
||||||
$model = new LoginForm();
|
$model = new LoginForm();
|
||||||
if ($this->populate($_POST, $model) && $model->login()) {
|
if ($model->load($_POST) && $model->login()) {
|
||||||
return Yii::$app->response->redirect(array('site/index'));
|
return $this->redirect(array('site/index'));
|
||||||
} else {
|
} else {
|
||||||
return $this->render('login', array(
|
return $this->render('login', array(
|
||||||
'model' => $model,
|
'model' => $model,
|
||||||
@@ -38,15 +38,15 @@ class SiteController extends Controller
|
|||||||
public function actionLogout()
|
public function actionLogout()
|
||||||
{
|
{
|
||||||
Yii::$app->user->logout();
|
Yii::$app->user->logout();
|
||||||
return Yii::$app->response->redirect(array('site/index'));
|
return $this->redirect(array('site/index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function actionContact()
|
public function actionContact()
|
||||||
{
|
{
|
||||||
$model = new ContactForm;
|
$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');
|
Yii::$app->session->setFlash('contactFormSubmitted');
|
||||||
return Yii::$app->response->refresh();
|
return $this->refresh();
|
||||||
} else {
|
} else {
|
||||||
return $this->render('contact', array(
|
return $this->render('contact', array(
|
||||||
'model' => $model,
|
'model' => $model,
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ class SiteController extends Controller
|
|||||||
public function actionLogin()
|
public function actionLogin()
|
||||||
{
|
{
|
||||||
$model = new LoginForm();
|
$model = new LoginForm();
|
||||||
if ($this->populate($_POST, $model) && $model->login()) {
|
if ($model->load($_POST) && $model->login()) {
|
||||||
return Yii::$app->response->redirect(array('site/index'));
|
return $this->redirect(array('site/index'));
|
||||||
} else {
|
} else {
|
||||||
return $this->render('login', array(
|
return $this->render('login', array(
|
||||||
'model' => $model,
|
'model' => $model,
|
||||||
@@ -39,15 +39,15 @@ class SiteController extends Controller
|
|||||||
public function actionLogout()
|
public function actionLogout()
|
||||||
{
|
{
|
||||||
Yii::$app->user->logout();
|
Yii::$app->user->logout();
|
||||||
return Yii::$app->response->redirect(array('site/index'));
|
return $this->redirect(array('site/index'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function actionContact()
|
public function actionContact()
|
||||||
{
|
{
|
||||||
$model = new ContactForm;
|
$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');
|
Yii::$app->session->setFlash('contactFormSubmitted');
|
||||||
return Yii::$app->response->refresh();
|
return $this->refresh();
|
||||||
} else {
|
} else {
|
||||||
return $this->render('contact', array(
|
return $this->render('contact', array(
|
||||||
'model' => $model,
|
'model' => $model,
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ to a model. For example,
|
|||||||
|
|
||||||
```php
|
```php
|
||||||
$model = new Post;
|
$model = new Post;
|
||||||
if ($this->populate($_POST, $model)) {...}
|
if ($model->load($_POST)) {...}
|
||||||
// which is equivalent to:
|
// which is equivalent to:
|
||||||
if (isset($_POST['Post'])) {
|
if (isset($_POST['Post'])) {
|
||||||
$model->attributes = $_POST['Post'];
|
$model->attributes = $_POST['Post'];
|
||||||
|
|||||||
@@ -637,6 +637,28 @@ class Model extends Component implements \IteratorAggregate, \ArrayAccess
|
|||||||
return $attributes;
|
return $attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Converts the object into an array.
|
||||||
* The default implementation will return [[attributes]].
|
* The default implementation will return [[attributes]].
|
||||||
|
|||||||
@@ -83,4 +83,34 @@ class Controller extends \yii\base\Controller
|
|||||||
}
|
}
|
||||||
return Yii::$app->getUrlManager()->createUrl($route, $params);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user