After a successful login, the url does not change in yii2-app-basic

After a successful login, I was going to about the page. This is normal.

But the URL does not change on the About page. This is the same as the login page, but the content of the page is the page.

SiteController.php

public function actionLogin()
{
    if (!\Yii::$app->user->isGuest) 
    {
        return $this->goHome();
    }

    $model = new LoginForm();
    if ($model->load(Yii::$app->request->post())) 
    {
        return $this->render('about'); // Here
    }

    return $this->render('login', [
        'model' => $model,
    ]);
}

The url is the same as http://localhost/myProject/yii/web/index.php?r=site%2Flogin. It should behttp://localhost/mylawsuit/yii/web/index.php?r=site%2Fabout

So how to change the url after login.? Thanks in advance.

+4
source share
4 answers

Instead of rendering, aboutyou should just use redirection:

return $this->redirect(['about']);
+5
source

Your response:

if ($model->load(Yii::$app->request->post())) 
{
     $this->redirect(['about']); // change here to this
}
+1
source

Yii::$app->request->referrer Yii Documentation

+1

You rendering about viewin actionLogin. obviously yours URLwill be logged in and have content about view.

Try something like this.

public function actionLogin()
{
    if (!\Yii::$app->user->isGuest) 
    {
        return $this->goHome();
    }

    $model = new LoginForm();
    if ($model->load(Yii::$app->request->post())) 
    {
        return $this->redirect('about'); // Change it to redirect
    }

    return $this->render('login', [
        'model' => $model,
    ]);
}
public function actionAbout()
{
   return $this->render('about');
}

Hope this helps!

+1
source

Source: https://habr.com/ru/post/1611418/


All Articles