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
06fa0259
Commit
06fa0259
authored
Apr 29, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added Controller::populate().
parent
f5f3d2e4
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
8 deletions
+34
-8
SiteController.php
app/protected/controllers/SiteController.php
+5
-7
LoginForm.php
app/protected/models/LoginForm.php
+1
-1
Controller.php
framework/base/Controller.php
+28
-0
No files found.
app/protected/controllers/SiteController.php
View file @
06fa0259
<?php
use
yii\web\Controller
;
use
app\models\LoginForm
;
use
app\models\User
;
class
SiteController
extends
\yii\web\
Controller
class
SiteController
extends
Controller
{
public
function
actionIndex
()
{
...
...
@@ -13,16 +13,14 @@ class SiteController extends \yii\web\Controller
public
function
actionLogin
()
{
$model
=
new
LoginForm
();
if
(
isset
(
$_POST
[
$model
->
formName
()]))
{
$model
->
attributes
=
$_POST
[
$model
->
formName
()];
if
(
$model
->
login
())
{
if
(
$this
->
populate
(
$_POST
,
$model
)
&&
$model
->
login
())
{
Yii
::
$app
->
getResponse
()
->
redirect
(
array
(
'site/index'
));
}
}
}
else
{
echo
$this
->
render
(
'login'
,
array
(
'model'
=>
$model
,
));
}
}
public
function
actionLogout
()
{
...
...
app/protected/models/LoginForm.php
View file @
06fa0259
...
...
@@ -42,7 +42,7 @@ class LoginForm extends Model
{
if
(
$this
->
validate
())
{
$user
=
User
::
findByUsername
(
$this
->
username
);
Yii
::
$app
->
getUser
()
->
login
(
$user
);
Yii
::
$app
->
getUser
()
->
login
(
$user
,
$this
->
rememberMe
?
3600
*
24
*
30
:
0
);
return
true
;
}
else
{
return
false
;
...
...
framework/base/Controller.php
View file @
06fa0259
...
...
@@ -303,6 +303,34 @@ class Controller extends Component
}
/**
* Populates one or multiple models from the given data array.
* @param array $data the data array. This is usually `$_POST` or `$_GET`, but can also be any valid array.
* @param Model $model the model to be populated. If there are more than one model to be populated,
* you may supply them as additional parameters.
* @return boolean whether at least one model is successfully populated with the data.
*/
public
function
populate
(
$data
,
$model
)
{
$success
=
false
;
if
(
!
empty
(
$data
)
&&
is_array
(
$data
))
{
$models
=
func_get_args
();
array_shift
(
$models
);
foreach
(
$models
as
$model
)
{
/** @var Model $model */
$scope
=
$model
->
formName
();
if
(
$scope
==
''
)
{
$model
->
attributes
=
$data
;
$success
=
true
;
}
elseif
(
isset
(
$data
[
$scope
]))
{
$model
->
attributes
=
$data
[
$scope
];
$success
=
true
;
}
}
}
return
$success
;
}
/**
* Renders a view and applies layout if available.
*
* The view to be rendered can be specified in one of the following formats:
...
...
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