public function actionLogin()
{
if (Yii::app()->user->isGuest) {
$model=new UserLogin;
// collect user input data
if(isset($_POST['UserLogin']))
{
$model->attributes=$_POST['UserLogin'];
// validate user input and redirect to previous page if valid
if($model->validate()) {
$this->lastViset();
if (strpos(Yii::app()->user->returnUrl,'/index.php')!==false)
$this->redirect(Yii::app()->controller->module->returnUrl);
else
$this->redirect(Yii::app()->user->returnUrl);
}
}
// display the login form
$this->render('/user/login',array('model'=>$model));
} else
$this->redirect(Yii::app()->controller->module->returnUrl);
}
From the PHP manual, I can learn $_GET[‘name001’], the name001 is the id = name001 inside a HTML tag, e.g.: <form id = “name001” method = GET>…</form>, but in Yii, it seems totally different. I can’t find any id attribute in any tag in the rendered HTML page.
My question is why it has to be ‘UserLogin’ inside the $_POST[‘UserLogin’], why not ‘MyUser0001’? Where else ‘UserLogin’ is defined, that we have to use it here?
Nettrinity, to give you an idea how it works, open up a new php file called post.php and copy the following bit into it (I don’t care about the html headers, as it’s just a real quick demonstration):
If you fill out the forms with first name John, last name Doe and favorite meal Lasagna and then submit the form, you should get the form back with the following var_dump() at the top of the page:
In your example, the model is called UserLogin and therefore you check if $_POST[‘UserLogin’] has been set. You can also see the same thing in the contact form of a fresh Yii application. There the model is called LoginForm and the login action in the controller must check if that model was set in $_POST. If it was, it means that the form was submitted.
It is possible that a page contains two or more forms. For example, a search form and a login form. By using the model-name method, you can find out which form was submitted: search, or login.
First, thank you very much for such a detailed explanation!
I did find a LoginForm model, and $_POST is always there, and auto populated after the form is submitted. The name attribute in each input tag is auto populated by Yii with modelName[dbTableColumnName]. After submission, $_POST is a nested array. Then you can work with $_POST[modelName] or $_POST[modelName][dbTableColumnName].