Laravel中间件顺序(中间件优先级).使用Postgres的多租户 [英] Laravel order of middleware (Middleware Priority). Multi-tenant using Postgres

查看:329
本文介绍了Laravel中间件顺序(中间件优先级).使用Postgres的多租户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

web.php 中,随着发出HTTP请求的子域类型,我已经在中间件中切换了Postgres模式.这样:

In web.php I've switched Postgres schemas in middleware as the subdomain type of HTTP request is made. This way:

Route::group(
    [
        'domain'     => '{tenant}.' . config('app.url'),
        'middleware' => 'select-schema'
    ],
    function () {
        $this->get('/', 'HomeController@index')->middleware('auth');
    }
);

select-schema 中间件中,我这样做.这可以正常工作. (不用担心)

In select-schema middleware, I do something like this. This works correctly. (don't worry)

DB::select('SET search_path TO ' . {tenant});

我的主要问题是:对于public模式和任何individual tenant,我都有不同的migrations.在individual tenant中,我有users表.一旦我登录,就会弹出此错误.

My main problem is that: I've different migrations for public schema and for any individual tenant. In individual tenant I have users table. As soon I'm logged in it pop up this error.

SQLSTATE [42P01]:未定义表:7错误:关系用户"不存在

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "users" does not exist

主要问题是

$this->get('/', 'HomeController@index')->middleware('auth');

模型运行良好,但是中间件authselect-schema

The model works well but middleware auth execute first before select-schema

如何订购? select-schema然后auth

推荐答案

我找到了解决方案, 为此,在App\Kernel中有一个叫做$middlewarePriority的东西.

I've found the solution, For this, there's something called $middlewarePriority in App\Kernel.

添加此内容可以帮助我解决问题.

Adding this help me solve the problem.

/**
 * Responsible for prioritizing the middleware
 *
 * @var array
 */
protected $middlewarePriority = [
    \App\Http\Middleware\SwitchSchema::class,
];

我已从此链接获得解决方案.

I've got solution from this link.

https://github.com/laravel/framework/issues/19565

这篇关于Laravel中间件顺序(中间件优先级).使用Postgres的多租户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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