gpt4 book ai didi

php - Yii 框架 undefined variable $model

转载 作者:搜寻专家 更新时间:2023-10-31 21:14:37 49 4
gpt4 key购买 nike

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

<?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;
}
}

我们遇到的问题是,当我们在主页上使用有效凭据登录时,会加载一个新的登录表单,但不会指示用户已通过身份验证。我们还看到语法错误,指出登录表单中的变量 $model 未定义。

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

如何解决上述问题?

最佳答案

首先,我注意到您将 $model 作为 User Object 的类型传递,而不是 LoginForm Object。首先改变那个。其次,因为 user 作为 model 发送(不确定你的 User 模型中有什么)

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

关于php - Yii 框架 undefined variable $model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11744412/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com