Laravel Multi Tenant - Override tenant initialization

66 Views Asked by At

I have an application that works in multi tenant, in Laravel.

In the moment it initiates the Tenants using the InitializeTenancyByPath middleware

The routes are something like:

www.myurl.pt/{tenant}/url

The middleware uses the {tenant} variable and starts the correct tenant. My routes.php are like this:

\Route::group([
            'prefix' => '{tenant}/admin/exports', 
            'middleware' => 
                [
                    "auth:sanctum", 
                    InitializeTenancyByPath::class
                ], 
            'namespace' => 'App\Domains\Exports\Actions'
            ], function () {

                \Route::name("admin.exports.exports")->get("/exports", "Export");

                \Route::name("admin.exports.download")->get("{file}/download", "Download");
    
                \Route::name("admin.exports.delete")->delete("file/{path}", "Delete");
        });

In order to do some changes it was asked if the url can be something like:

www.myurl.pt/{encoded_string}/url

The encoded string will be a safety code. We dont want to place the domain name. In fact, we want to place an encoded string. The application should read that encoded string, make a search in the database and initiate the Tenant.

Is there a way that allows me to Override the InitializeByPath middleware or use other?

I was reading about InitializeTenancyByRequestData but I cant find some examples.

Is there a way to create a middleware or override the existing one and do something similar like the following code, using the encoded_string variable in the url?

$encoded_string = request()->tenant;
$tenant = \DB::table('tenants')->where('encoded', $encoded_string)->first();
tenancy()->initialize($tenant);
0

There are 0 best solutions below