我是yii2的新手,现在我正在使用自定义登录,我做了所有登录代码,但是当我通过索引操作进入主页时,我没有得到任何会话,这是我的代码
public function actionIndex()
{
$session = Yii::$app->session;
if ($session->isActive) {
echo 'sdsd'; die;
}
return $this->render('index');
}
public function actionLogin()
{
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new User();
//if ($model->load(Yii::$app->request->post()) && $model->login()) {
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$userName = $_POST['User']['UserName'];
$password = $_POST['User']['Password'];
$condition = "UserName = '".$userName."' AND Password = '".md5($password)."' AND Status = '1' AND UserType = '1' ";
$loginData = User::find()->where($condition)->all();
if(count($loginData)>0) {
$username = $loginData[0]->UserName;
$userID = $loginData[0]->UserID;
$session = Yii::$app->session;
$session->set('userName',$username);
$session->set('userID',$userID);
$this->redirect('index');
} else {
Yii::$app->session->setFlash('error', 'Username or password is incorrect');
}
}
return $this->render('login', [
'model' => $model,
]);
}
即使在登录后仍然认为我是最忠诚的用户,Yii::$app->user->isGuest
我现在需要做些什么改变?
答案 0 :(得分:1)
您没有在Yii App中登录用户
您不需要设置会话,您必须为此功能分配适当的值
return \Yii::$app->getUser()->login($yourUser, $yourRememberMe ? $this->module->rememberFor : 0);
请参阅此doc for user和login