简体   繁体   中英

Can't log in after rendering login form in YII2

I wanna ask about my problem. Before it I wanna tell you what I do, I have RenunganHarianController and I made actionIndex like this. I wanna render the non-logged-in user to login form

public function actionIndex()
{
    $model = new LoginForm();
        //check user Guest or not
        if(Yii::$app->user->isGuest) {
        return $this->render('/site/login', [
            'model' => $model,
        ]);
    } 
    $searchModel = new RenunganHarianSearch();
    $dataProvider = $searchModel->search(Yii::$app->request->queryParams);        
    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

It's work, but when I try login from 'LoginForm' that rendered from that controller, I can't be logged in but if I try login from normal way, I can do that. Please help me, thank you

I am guessing that in your site/login view you have an ActiveForm with no specific action. If that is so, then the form's action is the same url that has been called, in your case yourcontroller/index (the actionIndex you have posted). The problem is that you have no logic in that action to handle te login process, which I guess is in your SiteController , actionLogin . You have two options here:

  1. Redirect the user to site/login insted of rendering the view in your actionIndex
  2. In your site/login view, specify the action for the form as Url::to('/site/login')

And yes, please, as suggested do not use capital letters as you did in the title.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM