Yii2 Advanced-前端和主站点之间的会话(www的前端副本) [英] Yii2 Advanced - Share session between frontend and mainsite (duplicate of frontend for www)

查看:93
本文介绍了Yii2 Advanced-前端和主站点之间的会话(www的前端副本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的会员系统: Yii2会员系统.您可以参考该文章以获取完整的详细信息,也可以安装它以解决该问题.

I have a custom members system that I have made: Yii2 Members System. You can refer to that for full details, or install it so you can work with it for this question.

正如Yii2所提供的,我有 frontend backend ,并做了一些修改以分隔会话/cookie,以便 backend 与管理员模型,并从 admin 表中提取.类似于旧的传统成员系统.

I have frontend and backend just as Yii2 provides, with a few modifications to separate the sessions/cookies so that backend works with the Admin model and pulls from an admin table. Similar to old traditional member systems.

mainsite 基本上是 frontend 的副本,它的作用是成为主要网站.当您访问 www.site.com site.com 时会得到什么.

mainsite is basically a clone of frontend and it's role is to be the main website. What you get when you go to www.site.com or site.com.

以下是这3个应用程序及其示例域:

Here are the 3 apps and their example domains:

  • 主站点 = www.site.com或site.com
  • 前端 = users.site.com
  • 后端 = admin.site.com
  • mainsite = www.site.com or site.com
  • frontend = users.site.com
  • backend = admin.site.com

当用户登录( users.site.com/site/login )并返回首页( mainsite 时,位于 www.site.com)),我希望它知道他们已登录并显示其用户名.就像默认情况下前端在高级应用程序中的运行方式一样.

When a user logs in (users.site.com/site/login) and go back to the homepage (mainsite at www.site.com) I want it to know they are logged in and show their username. Just like how frontend operates by default from the advanced app.

到目前为止,我登录并返回主站点,它只是读取发生内部服务器错误..它看起来不像Yii错误,但是服务器错误?当我在运行时下查看Yii日志时,它提到了访问控制:

From what I have so far, I login and head back to the mainsite and it just reads An internal server error occurred.. It doesn't look like a Yii error, but a server error? When I look in the Yii logs under runtime, it mentions access control:

2017-04-14 13:38:25 [127.0.0.1][1][-][error][yii\web\HttpException:403] exception 'yii\web\ForbiddenHttpException' with message 'You are not allowed to perform this action.' in /Applications/XAMPP/xamppfiles/htdocs/yii2-members-system/vendor/yiisoft/yii2/filters/AccessControl.php:151


这是我的一些配置.


Here are some of my configs.

mainsite/config/main.php

'components' => [
    'assetManager' => [
        'bundles' => false,
    ],
    'request' => [
        'csrfParam' => '_csrf-mainsite',
    ],
    'user' => [
        'class' => 'common\components\User',
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => true,
        'identityCookie' => [
            'name' => '_identity-frontend',
            'httpOnly' => true,
            'domain' => '.yii2-members-system.dev',
        ],
    ],
    'session' => [
        'name' => 'advanced-frontend',
        'cookieParams' => [
            'domain' => '.yii2-members-system.dev',
            'httpOnly' => true,
        ],
    ],
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
        ],
    ],
],

frontend/config/main.php

'components' => [
    'assetManager' => [
        'bundles' => false,
    ],
    'request' => [
        'csrfParam' => '_csrf-frontend',
    ],
    'user' => [
        'class' => 'common\components\User',
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => true,
        'identityCookie' => [
            'name' => '_identity-frontend',
            'httpOnly' => true,
            'domain' => '.yii2-members-system.dev',
        ],
    ],
    'session' => [
        'name' => 'advanced-frontend',
        'cookieParams' => [
            'domain' => '.yii2-members-system.dev',
            'httpOnly' => true,
        ],
    ],
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
        ],
    ],
],

虚拟主机

<VirtualHost *:80>
    ServerName yii2-members-system.dev
    ServerAlias yii2-members-system.dev
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/yii2-members-system/mainsite/web"
    ErrorLog "logs/mainsite.yii2-members-system.dev-error_log"
    CustomLog "logs/mainsite.yii2-members-system.dev-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName yii2-members-system.dev
    ServerAlias admin.yii2-members-system.dev
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/yii2-members-system/backend/web"
    ErrorLog "logs/admin.yii2-members-system.dev-error_log"
    CustomLog "logs/admin.yii2-members-system.dev-access_log" common
</VirtualHost>

<VirtualHost *:80>
    ServerName yii2-members-system.dev
    ServerAlias users.yii2-members-system.dev
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/yii2-members-system/frontend/web"
    ErrorLog "logs/users.yii2-members-system.dev-error_log"
    CustomLog "logs/users.yii2-members-system.dev-access_log" common
</VirtualHost>

推荐答案

您可以在子域或主域之间共享会话.按照yii的配置,您需要在 frontend/config/main.php mainsite/config/main.php 中进行如下配置.

You can share session between subdomain or main domain. As per yii configuration, you need to configure like below in frontend/config/main.php and mainsite/config/main.php.

'request' => [
    'csrfParam' => '_csrf-app',
],
'user' => [
    'identityClass' => 'common\models\User',
    'enableAutoLogin' => true,
    'identityCookie' => ['name' => '_identity', 'httpOnly' => true, 'domain'=>'.yii2-members-system.dev', 'path'=>'/'],
],
'session' => [
    'name' => 'sessionName',
    'savePath'=> __DIR__ . '/../../sessionTmp'
],

在此配置中,会话名称,会话保存路径,身份cookie路径和域应与其他子域匹配,以在两个yii应用程序之间共享会话.

In this configuration, session name, session save path, identity cookie path and domain should match with other subdomain to share session between two yii app.

因此,在您的应用程序内创建一个tmp文件夹,并将其指向会话保存路径.像上面一样.

So create one tmp folder inside your app and point it to session save path. like above.

我希望这会有所帮助.

这篇关于Yii2 Advanced-前端和主站点之间的会话(www的前端副本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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