Yii Framework未定义变量$ model

时间:2012-07-31 16:07:17

标签: php yii

我有一个标准的登录表单如下:

<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
    'validateOnSubmit'=>true,
),
)); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>

<div class="row">
    <?php echo $form->labelEx($model,'username'); ?>
    <?php echo $form->textField($model,'username'); ?>
    <?php echo $form->error($model,'username'); ?>  
</div>

<div class="row">
    <?php echo $form->labelEx($model,'password'); ?>
    <?php echo $form->passwordField($model,'password'); ?>
    <?php echo $form->error($model,'password'); ?>

</div>

<div class="row rememberMe">
    <?php echo $form->checkBox($model,'rememberMe'); ?>
    <?php echo $form->label($model,'rememberMe'); ?>
    <?php echo $form->error($model,'rememberMe'); ?>
</div>

<div class="row buttons">
    <?php echo CHtml::submitButton('Login'); ?>
</div>

<?php $this->endWidget(); ?>

我的UserIdentity类是:

public function authenticate()
    {
//                $username = $this->username;
//                $password = $this->password;

            $user = Users::model()->findbyAttributes(array($username=>$this->username));
            if($user === NULL){
                    $this->errorCode=self::ERROR_UNKNOWN_IDENTITY;


            }else if ($user->password !== md5($this->password)){    
//                        $this->username = $user->username;
//                       sess('SESS_USER_INFO', $user->attributes);
//                        $this->errorCode=self::ERROR_NONE;
        //invalid password
        $this->errorCode=self::ERROR_PASSWORD_INVALID;
            }
            else {
                $this->errorCode=self::ERROR_NONE;
            }
            return !$this->errorCode;
    }
}

我的UserController.php actionLogin如下:

public function actionLogin()
{
    $model= new Users();
//      Yii::app()->sessiion[username]= Yii::app()->model($username);
//      Yii:app()->session[password] = Yii::app()->model($password);
    // if it is ajax validation request
    if(isset($_POST['ajax']))
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
//                print_r($_POST['Users']);
    // collect user input data
    $form =  new LoginForm;
//      if(isset($_POST['Users']))
//      {
//          $model->attributes=$_POST['Users'];     

        // validate user input and redirect to the previous page if valid
//          if($model->login())
//              $this->redirect(Yii::app()->user->getReturnUrl('index'));
//      }
    // display the login form
//      $this->render('login',array('model'=>$model));
    if(isset($_POST['LoginForm'])){
    $form->attributes=$_POST['LoginForm'];
    if($form->validate() && $form->login()) $this->redirect(Yii::app()->user->returnUrl);
    }
    $this->render('login',array('model'=>$model));
}

LoginForm模型如下:

class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;

private $_identity;

/**
 * Declares the validation rules.
 * The rules state that username and password are required,
 * and password needs to be authenticated.
 */
public function rules()
{
    return array(
        // username and password are required
        array('username, password', 'required'),
        // rememberMe needs to be a boolean
        array('rememberMe', 'boolean'),
        // password needs to be authenticated
        array('password', 'authenticate'),
    );
}

/**
 * Declares attribute labels.
 */
public function attributeLabels()
{
    return array(
        'rememberMe'=>'Remember me next time',
    );
}

/**
 * Authenticates the password.
 * This is the 'authenticate' validator as declared in rules().
 */
public function authenticate($attribute,$params)
{
    if(!$this->hasErrors())
    {
//          $this->_identity=new UserIdentity($this->username,$this->password);
//          if(!$this->_identity->authenticate())
//              $this->addError('password','Incorrect username or password.');
        $identity = new UserIdentity($this->username, $this->password);
        $identity->authenticate();
        switch($identity->errorCode)
        {
            case UserIdentity::ERROR_NONE:
                $duration=$this->rememberMe ? 3600*24*30 : 0;  //30 days
                Yii::app()->user->login($identity,$duration);
                break;
            case UserIdentity::ERROR_USERNAME_INVALID:
                $this->addError('username', 'Username is incorrect.');
                break;
            default; // UserIdentity:ERROR_USERNAME_INVALID
                $this->addError('password','Password is incorrect.');
                break;
        }           
    }
}

/**
 * Logs in the user using the given username and password in the model.
 * @return boolean whether login is successful
 */
public function login()
{
    if($this->_identity===null)
    {
        $this->_identity=new UserIdentity($this->username,$this->password);
        $this->_identity->authenticate();
    }
    if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
    {
        $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
        Yii::app()->user->login($this->_identity,$duration);
        return true;
    }
    else
        return false;
}
}

我们遇到的问题是,当我们在主页上使用有效的证件登录时,会加载一个新的登录表单,而不会指示用户是否已经过身份验证。我们还看到语法错误,表明可变$模型在登录表单中未定义。

UPDDATE:我们不想使用/ views / site / login,而是使用/ views / users / login并将其连接到UserController。

我如何纠正上述问题?

1 个答案:

答案 0 :(得分:2)

首先,我注意到您将$model作为User Object的类型而不是LoginForm Object传递的内容很少。先改变一下。其次,因为user被发送为model(不确定您的用户模型中的内容)

isset($_POST['LoginForm'])将始终为false,并会再次转到登录表单