1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 22:54:03 +00:00

Load the middleware from the SP, fix #3

This commit is contained in:
Samuel Štancl 2019-01-20 17:33:10 +01:00
parent 94aee1e485
commit 89d97a89ab
2 changed files with 9 additions and 17 deletions

View file

@ -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;

View file

@ -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
]);
}
/**