Php Yii框架:UserLoginWidget

Php Yii框架:UserLoginWidget,php,dialog,yii,Php,Dialog,Yii,我试图在CWidget和cguidialog的帮助下重新创建用户登录小部件 当我点击登录链接时,对话框应该会打开登录表单 我使用了以下示例:, 我大部分都明白,但我在这一点上被卡住了。不确定这部分代码的含义: public function renderContent() { $form=new User; if(isset($_POST['user'])) { $form->attributes=$_POST['user']; if

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

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

我使用了以下示例:, 我大部分都明白,但我在这一点上被卡住了。不确定这部分代码的含义:

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));
}
在此之后,在component/view/login.php中有一个名为login.php的文件,您可以像这样在视图中访问它

<?php $this->widget('your class name'); ?>


我对这部分感到困惑$form=new
用户不确定放在那里的类。我没有一个用户类。好吧,你可以不用类,只使用POST变量和直接检查访问,但是框架的设计就是在使用表单时这样工作的,所以创建一个用户访问模型不是一个坏主意。如果您使用过,您可以在那里检查用户类是如何从CFormModel扩展和使用的。现在我得到了这个错误:UserLoginWidget找不到“login”视图。我的登录页面位于views/site/login.php下。我应该给它一个直接的路径吗?如果你打算将登录表单作为一个小部件使用,这意味着你将在项目的其他部分重用它,所以小部件所需的所有文件都必须在他的文件夹中,并在中查看。是的,这是我不理解的,我尝试将login.php放在与UserLoginWidget相同的目录中,但仍然得到相同的错误。编辑:啊,修好了。在“模型”文件夹中创建了一个细分曲面。泰。
<?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
}