简体   繁体   English

Laravel中间件的顺序(Middleware Priority)。 使用Postgres的多租户

[英]Laravel order of middleware (Middleware Priority). Multi-tenant using Postgres

In web.php I've switched Postgres schemas in middleware as the subdomain type of HTTP request is made. web.php中 ,随着发出HTTP请求的子域类型,我在中间件中切换了Postgres模式。 This way: 这条路:

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

In select-schema middleware, I do something like this. 选择模式中间件中,我这样做。 This works correctly. 这可以正常工作。 (don't worry) (不用担心)

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

My main problem is that: I've different migrations for public schema and for any individual tenant . 我的主要问题是:对于public架构和任何individual tenant ,我都有不同的migrations In individual tenant I have users table. individual tenant我有users表。 As soon I'm logged in it pop up this error. 一旦我登录,就会弹出此错误。

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "users" does not exist SQLSTATE [42P01]:未定义表:7错误:关系“用户”不存在

The main issue is 主要问题是

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

The model works well but middleware auth execute first before select-schema 该模型运行良好,但select-schema之前先执行中间件auth

How do I order? 我该如何订购? select-schema then auth select-schema然后auth

I've found the solution, For this, there's something called $middlewarePriority in App\\Kernel . 我已经找到了解决方案,为此,在App\\Kernel有一个名为$middlewarePriority的东西。

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 https://github.com/laravel/framework/issues/19565

Have you tried wrapping your routes in the tenant group with another group? 您是否尝试过将路由与其他组一起包装在租户组中? See if this works: 查看是否可行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM