hipsterjazzbo / Landlord

A simple, single database multi-tenancy solution for Laravel 5.2+
MIT License
613 stars 138 forks source link

Great Library but how do i implement this for subdomain Tenancy? #4

Closed samsoft00 closed 8 years ago

samsoft00 commented 8 years ago

Hello @HipsterJazzbo, how how do implement this for a subdomain tenancy? Thanks

phaberest commented 8 years ago

I'm on the same boat, trying to find out

samsoft00 commented 8 years ago

@phaberest i ended up using Ruby on rails for my development, hope @HipsterJazzbo is able to provide this option

dlimars commented 8 years ago

@samsoft00 @phaberest i'm developing something with subdomains, check out here https://github.com/dlimars/laravel-tenant-subdomain

phaberest commented 8 years ago

Thanks @dlimars , I'll look forward into it :+1:

By the way I'm getting to the point with something like this

    # my app full domain without subdomains
    $domain  = str_replace(['http://', 'https://', '//'], '', env('APP_URL'));
    # get the actual subdomain
    $account = array_shift((explode(".", $_SERVER['HTTP_HOST'])));
    # get the (probably fake) subdomain of my app domain
    $default = array_shift((explode(".", $domain)));
    # compare to check if I'm under a tenant subdomain or the root domain
    if ($account != $default) {
        $managerId = Manager::where('url_part', '=', $account)
            ->firstOrFail()->id;

        if (!$managerId) {
            abort(404);
        }
        # save the subdomain and the id of my tenant to use them in app and Landlord
        set_meta('manager_url', $account);
        set_meta('manager_id', $managerId);
    }
    set_meta('account::domain', '{account}.'.$domain);
    set_meta('app::url', $domain);

in my AppServiceProvider where Manager is actually my tenant.

Still working on it, but at least it's a solution...

PS: set_meta() is a function related to OrchestraPlatform which I'm using as a base for my project (still Laravel, anyway). Consider it as an alternative for session().

PPS: In case anyone decided to use my code, to avoid cli issue with Undefined index: HTTP_HOST edit $account as follows.

    $account = isset($_SERVER['HTTP_HOST'])
        ? array_shift((explode(".", $_SERVER['HTTP_HOST'])))
        : $default;

I suppose it will make some noise when testing with phpunit too.

hipsterjazzbo commented 8 years ago

Hey guys, I'd do something like this in a middleware:

public function handle($request, Closure $next)
{   
    $pieces = explode('.', $request->getHost());
    $subdomain = $pieces[0];

    // Use the subdomain to get a tenant id somehow
    $tenantId = '';
    Landlord::addTenant('$tenantColumn, $tenantId);

    return $next($request);
}

Maybe I'll do up some example middlewares...

jcs224 commented 8 years ago

Just thought I'd point out, wildcard subdomain route prefixes are supported in Laravel...you could probably use that to forward to the middleware?

https://laravel.com/docs/5.2/routing#route-group-sub-domain-routing

phaberest commented 8 years ago

@jcs224 that's actually what my get_meta('account::domain') does in routes ;)

jcs224 commented 8 years ago

@phaberest :+1: