DEV Community

Cover image for Orchid Platform meets multi-tenancy
Ostap Brehin
Ostap Brehin

Posted on • Edited on

Orchid Platform meets multi-tenancy

Orchid is a Platform for back-office applications on Laravel. With it, you can create beautiful apps faster, using only PHP code.

Tenancy is a feature, allows you to split data each user use (The way how SaaS works).

Here are a few steps you can use to integrate multi-tenancy for your orchid application.

1. Install tenancy package

For tenancy package, I will use stancl/tenancy.

Go to installation page and run 2 simple commands as written :)

composer require stancl/tenancy
php artisan tenancy:install
Enter fullscreen mode Exit fullscreen mode

2. Configure domain detection

2.1. Go to the config/tenancy.php and specify all your domains (local, production) in central_domains key.

Example:

    'central_domains' => [
        '127.0.0.1',
        'localhost',
        'businessmachine.wip',
        'businessmachine.local',
        'businessmachine.app',
    ],
Enter fullscreen mode Exit fullscreen mode

2.2. Move all your platform routes from routes/platform.php to routes/tenant.php

Note: if you want to add acess control, you can do it here by specifying middleware:

Route::middleware('auth', 'tenant.access')->group(function (){
    // Your content here
});
Enter fullscreen mode Exit fullscreen mode

Sample access-middleware:

class CheckTenantAccess
{
    public function handle(Request $request, Closure $next)
    {
        if (!auth()->user()->tenants->contains(tenant('id'))){
            return redirect()->route('workspaces.index');
        }
        return $next($request);
    }
}
Enter fullscreen mode Exit fullscreen mode

2.3 Add macro to get subdomain

Inside boot method in AppServiceProvider, add these lines:

use Illuminate\Http\Request;
...
Request::macro('subdomain', function () {
    return current(explode('.', $this->getHost()));
});
Enter fullscreen mode Exit fullscreen mode

2.4. Automatically add subdomain as a parameter to every platform route:

Create new middleware with content:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\URL;

class TenancyDomain
{
    public function handle(Request $request, Closure $next)
    {
        // Add current subdomain as a parameter to all route links
        URL::defaults(['subdomain' => $request->subdomain()]);

        // Do not inject 'subdomain' parameter in controllers
        $request->route()->forgetParameter('subdomain');

        return $next($request);
    }
}
Enter fullscreen mode Exit fullscreen mode

And register it in app/Http/Kernel.php:

class Kernel extends HttpKernel
...
    protected $middlewareGroups = [
        'web' => [
            ...
            \App\Http\Middleware\TenancyDomain::class,
        ],
...
Enter fullscreen mode Exit fullscreen mode

2.5 Go to config/platform.php, and update 'domain' line:

    'domain' => env('DASHBOARD_DOMAIN', '{subdomain}.yourdomain.test'),
    'prefix' => env('DASHBOARD_PREFIX', '/'),

Enter fullscreen mode Exit fullscreen mode

3. Move migrations

Move your migrations to a 'database/migrations/tenant' directory.

That's it! :)

Top comments (0)