Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After login successfully, URL not changing in yii2-app-basic

I was going to About Page after Successfully login. It's OK.

But, URL not changing to About Page. It is same as login Page, but the content of page is About 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,
    ]);
}

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

So, how to change URL after login.? Thanks in advance.

like image 959
Nana Partykar Avatar asked Oct 13 '15 11:10

Nana Partykar


4 Answers

Instead of rendering about view, you should simply use a redirection :

return $this->redirect(['about']);
like image 163
soju Avatar answered Oct 21 '22 20:10

soju


your answer:

if ($model->load(Yii::$app->request->post())) 
{
     $this->redirect(['about']); // change here to this
}
like image 1
Mohsen Avatar answered Oct 21 '22 22:10

Mohsen


You can use Yii::$app->request->referrer here a Yii Documentation

like image 1
Vahe Galstyan Avatar answered Oct 21 '22 21:10

Vahe Galstyan


You are rendering the about view in actionLogin. obviously your URL will be login & have content of 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 it will help!

like image 1
Husnain Aslam Avatar answered Oct 21 '22 21:10

Husnain Aslam