How to set separate homeUrl for authenticated users and guests in yii2

我与影子孤独终老i 提交于 2019-12-12 03:49:48

问题


I'm wondering is it possible to make different homeUrl for authenticated and guest users? I have such rules in SiteController.php

public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['logout', 'signup'],
            'rules' => [
                [
                    'actions' => ['signup'],
                    'allow' => true,
                    'roles' => ['?'],
                    'denyCallback' => function() {
                        return $this->redirect('/account');
                    }
                ],
                [
                    'actions' => ['logout', 'condition'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
            ],
        ],
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'logout' => ['post'],
            ],
        ],
    ];
}

回答1:


That shouldn't be much of a problem. My answer is structured as follows:

  1. Config for guest users
  2. Config for logged in users (overwriting)
  3. Alternatives

1. Config for guest users

This one is easy. Simply set the home URL to whatever you want your guest visitors to land on. Put this in your config:

//...
'homeUrl'=>'site/home-guests',
//...

2. Config for logged in users (overwriting)

A login happens on each and every page-load either via form submission, cookie (remember me), session or access token. You won't notice this since it happens automatically except for the actual form-login.

The yii\web\User-class has an event which will be triggered after every login. Its called yii\web\User::EVENT_AFTER_LOGIN. You can find the doc for the triggering method here.

To fulfill your requirement simply extend the user class and within the init()-method attach an event handler. If the event mentioned gets thrown, change the home URL. Done!

Heres the code of the custom user class:

class User extends \yii\web\User {
    //...
    public function init() {
        parent::init();

        //listen to after login event
        $this->on(static::EVENT_AFTER_LOGIN, function ($event) {
            Yii::$app->setHomeUrl(Url::to(['site/home-logged-in']));
        });
    }
    //...
}

For Yii to use your custom and extended class, simply tell it to within the config:

//...
'user'=>[
    'class'=>'app\components\User',    
],    
//...

3. Alternatives

If it's not about the URL but simply to show the users different contents depending on their login-status you have other / easier options:

  • show a different view when logged in but use the same action
  • redirect them within the action method
  • if/else within the actual home-view

Which one is right depends on your detailed requirement. This is difficult to tell with the information you provided.

Tell me if you have further questions. I'll be happy to help!



来源:https://stackoverflow.com/questions/40081311/how-to-set-separate-homeurl-for-authenticated-users-and-guests-in-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!