Advertisement
rubensrocha

Laravel Multi-Tenant Multi-DB Multi-Domain TenantServiceProv

Nov 13th, 2018
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Providers;
  4.  
  5. use Illuminate\Support\ServiceProvider;
  6. use Illuminate\Support\Facades\Route;
  7. use App\Tenant\ManagerTenant;
  8. use Illuminate\Support\Facades\View;
  9.  
  10. class TenantServiceProvider extends ServiceProvider
  11. {
  12.     /**
  13.      * Bootstrap services.
  14.      *
  15.      * @return void
  16.      */
  17.     public function boot()
  18.     {
  19.         $manager = app(ManagerTenant::class);
  20.         if ($manager->domainIsMain())
  21.         {
  22.             $this->registerTenantRoutes();
  23.             $this->registerTenantAdminRoutes();
  24.         }
  25.         //Auto detect admin views
  26.         View::addNamespace('admin', resource_path().'/views/admin/');
  27.     }
  28.  
  29.     private function registerTenantRoutes()
  30.     {
  31.         Route::group($this->tenantRouteConfiguration(), function () {
  32.             $route_file = config('tenant.routes.frontend');
  33.             if(!file_exists($route_file)){
  34.                 throw new \Exception(trans('tenant.tenant_route'));
  35.                 return;
  36.             }
  37.             require $route_file;
  38.         });
  39.     }
  40.  
  41.     private function registerTenantAdminRoutes()
  42.     {
  43.         Route::group($this->tenantAdminRouteConfiguration(), function () {
  44.             $admin_route_file = config('tenant.routes.backend');
  45.             if(!file_exists($admin_route_file)){
  46.                 throw new \Exception(trans('tenant.tenant_admin_route'));
  47.             }
  48.             require $admin_route_file;
  49.         });
  50.     }
  51.  
  52.     private function tenantRouteConfiguration()
  53.     {
  54.         return [
  55.             'namespace' => config('tenant.namespaces.frontend'),
  56.             'middleware' => config('tenant.middlewares.frontend'),
  57.         ];
  58.     }
  59.  
  60.     private function tenantAdminRouteConfiguration()
  61.     {
  62.         return [
  63.             'namespace' => config('tenant.namespaces.backend'),
  64.             'middleware' => config('tenant.middlewares.backend'),
  65.         ];
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement