新增admin_user表作为后台管理员,程序仿user表做,登陆时跳不去后台,求帮助! [ 求助交流 ]
1、登陆代码:
public function actionLogin()
{
$this->layout = 'guest';
$model = new LoginAdminForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
1、LoginAdminForm代码:
<?php
namespace common\models;
use Yii;
use yii\base\Model;
/**
* Login form
*/
class LoginAdminForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
/**
* @inheritdoc
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'username' => Yii::t('app', 'Username'),
'password' => Yii::t('app', 'Password'),
'rememberMe' => Yii::t('app', 'Remember Me'),
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, Yii::t('app', 'Incorrect username or password.'));
}
}
}
/**
* Logs in a user using the provided username and password.
*
* @return boolean whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
return false;
}
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = AdminUser::findByUsername($this->username);
}
return $this->_user;
}
}
错误:echo 111;可以打印出来,到了goback(),就直接跳回登陆页面。即跳转到/site/index ->site/login。
public function actionLogin()
{
$this->layout = 'guest';
$model = new LoginAdminForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {**echo 1111;exit;**
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
跟踪发现是一个属性没获取到:文件位置/vendor/yiisoft/yii2/web/respose.php
函数sendContent()的$this->stream是为null。
请帮忙!谢谢
共 0 条回复
没有找到数据。
lzg553665176
注册时间:2015-09-14
最后登录:2015-09-24
在线时长:0小时15分
最后登录:2015-09-24
在线时长:0小时15分
- 粉丝0
- 金钱15
- 威望0
- 积分15