Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
9cf82b3a
Commit
9cf82b3a
authored
Jun 26, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Model::load(). Added Controller::refresh() and redirect().
parent
71a156eb
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
66 additions
and
14 deletions
+66
-14
SiteController.php
apps/advanced/backend/controllers/SiteController.php
+3
-3
SiteController.php
apps/advanced/frontend/controllers/SiteController.php
+5
-5
SiteController.php
apps/basic/controllers/SiteController.php
+5
-5
upgrade-from-v1.md
docs/guide/upgrade-from-v1.md
+1
-1
Model.php
framework/yii/base/Model.php
+22
-0
Controller.php
framework/yii/web/Controller.php
+30
-0
No files found.
apps/advanced/backend/controllers/SiteController.php
View file @
9cf82b3a
...
...
@@ -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'
));
}
}
apps/advanced/frontend/controllers/SiteController.php
View file @
9cf82b3a
...
...
@@ -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
,
...
...
apps/basic/controllers/SiteController.php
View file @
9cf82b3a
...
...
@@ -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
,
...
...
docs/guide/upgrade-from-v1.md
View file @
9cf82b3a
...
...
@@ -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'
];
...
...
framework/yii/base/Model.php
View file @
9cf82b3a
...
...
@@ -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
...
...
framework/yii/web/Controller.php
View file @
9cf82b3a
...
...
@@ -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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment