在yii2中登录后重定向到另一个页面

时间:2017-07-01 19:17:46

标签: php yii2

在Yii中,当您默认登录时,它会重定向到索引页面。我希望当我登录Yii时,页面将重定向到另一个页面,而不是索引页面。所以任何人都可以帮助我。任何帮助或建议都会非常值得关注。我的actionLogin函数位于siteController.php

@Scenario6
Scenario: Search for an item using the search functionality
Given Navigate to "Amazon.com" home page.
When Search for "iphone 7"
Then The search results return the desired items
@Scenario7   
Scenario: Select a department
Given Navigate to "Amazon.com" home page.
When Hover over the Departments dropdown menu
And Select Books&Audible category
And From the flyout menu select Books
Then Your "Amazon.com: Books" page is displayed
@Scenario8  
Scenario: Browse "New for you" items
Given Navigate to "Amazon.com" home page.
When Go to "Amazon basics" and click on "Home"
Then Your "Amazon.com: Home & Kitchen" page is displayed

3 个答案:

答案 0 :(得分:2)

您可以重定向到您喜欢的页面

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

      $model = new LoginForm();
      if ($model->load(Yii::$app->request->post()) && $model->login()) {
         return $this->redirect(['/admin/index']);
      }
      return $this->render('login', [
          'model' => $model,
      ]);
  }

或者如果需要显示一个实例,例如:id = $ model-> id

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

      $model = new LoginForm();
      if ($model->load(Yii::$app->request->post()) && $model->login()) {
         return $this->redirect(['/admin/view',  'id' => $model->id]);
      }
      return $this->render('login', [
          'model' => $model,
      ]);
  }

答案 1 :(得分:0)

这是一个例子 打开您的views文件夹并打开站点文件夹并创建一个新页面而不是index.php,只需假设新页面是welcome.php并在您的控制器中通过欢迎替换索引,如下所示

   public function actionLogin() 
{
 if (!Yii::$app->user->isGuest) 
{ 
return $this->goHome(); 
}
 $model = new LoginForm(); 
if ($model->load(Yii::$app->request->post()) && $model->login())
 {
 return $this->goBack(); 
}
 return $this->render('welcome', [ 'model' => $model, ]); 
}

答案 2 :(得分:0)

更改登录页面

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

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect(['site/loader']);
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }

并且在您的控制器中,我认为站点控制器添加

public function actionLoader()
{
     return $this->render('yourPage');
}
希望这很有帮助。