tenancy / multi-tenant

Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously github.com/hyn/multi-tenant
https://tenancy.dev
MIT License
2.56k stars 394 forks source link

[Question] Tenancy on Single Domain? #910

Closed ReArmedHalo closed 4 years ago

ReArmedHalo commented 4 years ago

Just discovered Tenancy and I think it will solve an issue in an upcoming project.

Can I use Tenancy v5.6 (with Laravel 7) without utilizing hostnames and subdomains? I would use just the website model and my own identification code but does anyone see any issues long term? Just trying to get input before I dive deep into this project and find out it won't be possible to do "that one critical thing my app needs and now I have to start over since I can't use Tenancy" situtation.

For my test, I used explicit route model binding like below and that seemed to work.

// app/Providers/RouteServiceProvider.php

Route::bind('tenant', function ($value) {
    $environment = app(\Hyn\Tenancy\Environment::class);
    $website = \Hyn\Tenancy\Models\Website::whereUuid($value)->firstOrFail();
    $environment->tenant($website);
    //ddd(app(\Hyn\Tenancy\Environment::class)->tenant());
    return $environment->tenant();
});

My URLs would look like this: https://laravel7.test/app/{tenant}/blah => https://laravel7.test/app/418b8053-99aa-4064-a557-6d884ea3e213/blah

Super simple middleware saves me from having to add the tenant parameter to each route() call in my blade files:

public function handle($request, Closure $next)
{
    URL::defaults(['tenant' => $request->tenant]);

    return $next($request);
}

I mostly need database table separation for my project (storing audit log data from third-party sources) and Tenancy provides that functionality but I don't need separate storage, domains, etc. I am using OAuth instead of the primary Laravel Auth scaffold (this app is a consumer, I own the IDP app which is also in Laravel) so subdomain authentication would be a pain with redirect URLs. Based on #527 I think my answer would be yes? I will be utilizing background jobs so would TenantAwareJob still function properly? (I'll admit, I don't yet know how that trait works so it may be a mute point for my situation)

Thoughts? Thanks!

fletch3555 commented 4 years ago

Thoughts?

Yeah, if it works for you, it can't be wrong.

Your approach seems reasonable to me.