我正在尝试在CWidget和CJuiDialog的帮助下重新创建用户登录小部件。
当我单击登录链接时,对话框应打开登录表单。
我使用了以下示例:小部件,我理解大部分内容,但我在这一点上陷入困境。不确定这部分代码是什么意思:
public function renderContent()
{
$form=new User;
if(isset($_POST['user']))
{
$form->attributes=$_POST['user'];
if($form->validate() && $form->login()){
$url = $this->controller->createUrl('site/index');
$this->controller->redirect($url);
}
}
$this->render('login',array('form'=>$form));
}
带有解释的代码:
public function renderContent()
{
// Var to store data sent by client browser
$form=new User;
// Check if the post request has defined the user variable
if(isset($_POST['user']))
{
// fill the $form attributes with the values sent by the web form in the post request
$form->attributes=$_POST['user'];
// validate the form data and then check if the data is valid to login the user
// - the validate call is where the framework check if the data is valid
// against the model (e.g. user field must be text, not empty...)
// - the login call is where you should encode your user validation, check for validity against the database or whatever you want
if($form->validate() && $form->login()){
// create the url where the client browser is going to be redirected
$url = $this->controller->createUrl('site/index');
// render a 302 redirection to the new page
$this->controller->redirect($url);
}
}
// if the request doesn't contain the 'user' variable, or if the validation/login calls have failed, render again the form. In the case of errors, they'll be shown in the 'errorSummary' section.
$this->render('login',array('form'=>$form));
}
$this->render('login',array('form'=>$form));
在此之后,您将在组件/视图/登录中有一个名称为login.php的文件.php您可以在视图中像这样访问...
<?php $this->widget('your class name'); ?>
public function renderContent()
{
$form=new User;//create User model
if(isset($_POST['user']))// process when client submit (login)
{
$form->attributes=$_POST['user'];//add attribute from form in client
if($form->validate() && $form->login()){//validate and check login using User class created in first line of this function
$url = $this->controller->createUrl('site/index');//create url for redirect after login successfully
$this->controller->redirect($url);
}
}
$this->render('login',array('form'=>$form));//if it is not in submiting or login fail, we will go this code, it render the login form
}