0

我试图在 CWidget 和 CJuiDialog 的帮助下重新创建用户登录小部件。

当我单击登录链接时,对话框应打开登录表单。

我使用了以下示例:Widget,我了解其中的大部分内容,但此时我陷入了困境。不确定这部分代码是什么意思:

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));
}
4

3 回答 3

1
$this->render('login',array('form'=>$form));

在此之后,您在 component/view/login.php 中有一个名为 login.php 的文件,您可以像这样在视图中访问...

<?php $this->widget('your class name'); ?>
于 2012-05-28T11:02:18.917 回答
1

带解释的代码:

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));
}
于 2012-05-28T10:58:21.557 回答
0
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
}
于 2012-05-30T09:32:44.647 回答