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

痴心易碎 提交于 2019-12-10 00:01:10

问题


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

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. In individual tenant I have users table. As soon I'm logged in it pop up this error.

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

The main issue is

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

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

How do I order? select-schema then auth


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/44487764/laravel-order-of-middleware-middleware-priority-multi-tenant-using-postgres

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