我需要使用API登录。用于在$ _POST [ 'username']
上发送$ _POST [ 'password']
,actionLogin ()
的API。
public function actionLogin()
{
if (!Yii::app()->user->isGuest)
$this->redirect($this->createUrl('site/_allorders'));
$model = new LoginForm;
if (isset($_POST['LoginForm'])) {
$model->attributes = $_POST['LoginForm'];
if ($model->validate()) {
$this->redirect(Yii::app()->user->returnUrl);
}
}
$this->layout = 'mini';
$this->render('_loginForm', array('model' => $model));
}
方法$ model-> validate ()
通常会通过,但无法登录。我的错是什么或缺少什么?
答案 0 :(得分:0)
你需要在LoginForm类中登录方法,你有登录方法吗?!
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
return false;
}
}
并用于登录操作:
public function actionLogin()
{
if (!Yii::app()->user->isGuest)
$this->redirect($this->createUrl('site/_allorders'));
$model = new LoginForm;
if ($model->load(Yii::$app->request->post()) && $model->login()) {
$this->redirect(Yii::app()->user->returnUrl);
}
$this->layout = 'mini';
$this->render('_loginForm', array('model' => $model));
}
如果你最近加入yii2,请测试提前yii2项目是here .it有登录api