如何在 yii2 中为经过身份验证的用户和来宾设置单独的 homeUrl [英] How to set separate homeUrl for authenticated users and guests in yii2

查看:10
本文介绍了如何在 yii2 中为经过身份验证的用户和来宾设置单独的 homeUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以为经过身份验证的用户和来宾用户设置不同的 homeUrl?我在 SiteController.php 中有这样的规则

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'],
            ],
        ],
    ];
}

推荐答案

这应该不是什么大问题.我的回答结构如下:

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

  1. 来宾用户的配置
  2. 登录用户的配置(覆盖)
  3. 替代方案

1.来宾用户配置

这个很简单.只需将主页 URL 设置为您希望访客访问的任何内容.把它放在你的配置中:

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.登录用户的配置(覆盖)

登录通过表单提交、cookie(记住我)、会话或访问令牌在每个页面加载时发生.您不会注意到这一点,因为它会自动发生,除了实际的表单登录.

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.

yii\web\User-class 有一个事件,每次登录后都会触发.它被称为 yii\web\User::EVENT_AFTER_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.

要满足您的要求,只需扩展用户类并在 init() 方法中附加一个事件处理程序.如果提到的事件被抛出,请更改主页 URL.大功告成!

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']));
        });
    }
    //...
}

为了让 Yii 使用您的自定义和扩展类,只需在配置中告诉它:

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

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

3.替代方案

如果不是关于 URL 而只是根据用户的登录状态向用户显示不同的内容,您还有其他/更简单的选择:

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:

  • 登录时显示不同的视图,但使用相同的操作
  • 在操作方法中重定向它们
  • if/else 在实际主视图中

哪一个是正确的取决于您的详细要求.用您提供的信息很难判断这一点.

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!

这篇关于如何在 yii2 中为经过身份验证的用户和来宾设置单独的 homeUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆