From 89d97a89ab72c048f726db89fd4507b43dbb395e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 20 Jan 2019 17:33:10 +0100 Subject: [PATCH] Load the middleware from the SP, fix #3 --- README.md | 21 ++++----------------- src/TenancyServiceProvider.php | 5 +++++ 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index cbc10a5e..9bc4e72f 100644 --- a/README.md +++ b/README.md @@ -18,30 +18,17 @@ You won't have to change a thing in your application's code. composer require stancl/tenancy ``` -### Adding the `InitializeTenancy` middleware +### Configuring the `InitializeTenancy` middleware -Open `app/Http/Kernel.php` and make the following changes: +The `TenancyServiceProvider` automatically adds the `tenancy` middleware group which can be assigned to routes. You only need to make sure the middleware is top priority. -First, you want to create middleware groups so that we can apply this middleware on routes. -- Create a new middleware group in `$middlewareGroups`: - ```php - 'tenancy' => [ - \Stancl\Tenancy\Middleware\InitializeTenancy::class, - ], - ``` -- Create a new middleware group in `$routeMiddleware`: - ```php - 'tenancy' => \Stancl\Tenancy\Middleware\InitializeTenancy::class, - ``` -- Make the middleware top priority, so that it gets executed before anything else, thus making sure things like the database switch connections soon enough. +Open `app/Http/Kernel.php` and make the middleware top priority, so that it gets executed before anything else, making sure things like the database switch connections soon enough. ```php protected $middlewarePriority = [ \Stancl\Tenancy\Middleware\InitializeTenancy::class, ``` -#### Configuring the middleware - -When a tenant route is visited, but the tenant can't be identified, an exception can be thrown. If you want to change this behavior, to a redirect for example, add this to your `app/Providers/AppServiceProvider.php`'s `boot()` method. +When a tenant route is visited, but the tenant can't be identified, an exception is thrown. If you want to change this behavior, to a redirect for example, add this to your `app/Providers/AppServiceProvider.php`'s `boot()` method. ```php // use Stancl\Tenancy\Middleware\InitializeTenancy; diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index c3636189..c7eaf0b6 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -7,6 +7,7 @@ use Stancl\Tenancy\TenantManager; use Stancl\Tenancy\DatabaseManager; use Stancl\Tenancy\Commands\Migrate; use Stancl\Tenancy\Commands\Rollback; +use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; use Stancl\Tenancy\Commands\TenantList; use Stancl\Tenancy\Interfaces\StorageDriver; @@ -35,6 +36,10 @@ class TenancyServiceProvider extends ServiceProvider ], 'config'); $this->loadRoutesFrom(__DIR__ . '/routes.php'); + + Route::middlewareGroup('tenancy', [ + \Stancl\Tenancy\Middleware\InitializeTenancy::class + ]); } /**