-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #308 from spatie/add-route-cache-path-switcher
Add route cache path switcher
- Loading branch information
Showing
5 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ coverage | |
.phpunit.result.cache | ||
.idea | ||
.php-cs-fixer.cache | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
docs/using-tasks-to-prepare-the-environment/switching-route-cache-paths.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
title: Switching route cache paths | ||
weight: 3 | ||
--- | ||
|
||
Laravel comes with [route caching](https://laravel.com/docs/master/routing#route-caching) out of the box. By default | ||
all routes are cached, which means that the application will only load the routes once. This is great if your routes | ||
are static. However, if you're using dynamic routes, for example different routes for different tenants, you'll need | ||
to keep a separate route cache for each tenant. | ||
|
||
The `Spatie\Multitenancy\Tasks\SwitchRouteCacheTask` can switch the configured `APP_ROUTES_CACHE` environment variable to a tenant specific value: `bootstrap/cache/routes-v7-tenant-{$tenant->id}.php`. | ||
|
||
To use this task, you should uncomment it in the `switch_tenant_tasks` section of the `multitenancy` config file. | ||
|
||
```php | ||
// in config/multitenancy.php | ||
|
||
'switch_tenant_tasks' => [ | ||
\Spatie\Multitenancy\Tasks\SwitchRouteCacheTask::class, | ||
// other tasks | ||
], | ||
``` | ||
|
||
Finally but **most importantly**, you should use `php artisan tenant:artisan route:cache` to cache your routes instead of Laravel's default `route:cache` command. This will make sure a different route cache file is generated for each tenant. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Spatie\Multitenancy\Tasks; | ||
|
||
use Illuminate\Support\Env; | ||
use Spatie\Multitenancy\Models\Tenant; | ||
|
||
class SwitchRouteCacheTask implements SwitchTenantTask | ||
{ | ||
public function makeCurrent(Tenant $tenant): void | ||
{ | ||
Env::getRepository()->set('APP_ROUTES_CACHE', "bootstrap/cache/routes-v7-tenant-{$tenant->id}.php"); | ||
} | ||
|
||
public function forgetCurrent(): void | ||
{ | ||
Env::getRepository()->clear('APP_ROUTES_CACHE'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Spatie\Multitenancy\Tests\Feature\Tasks; | ||
|
||
use Spatie\Multitenancy\Models\Tenant; | ||
use Spatie\Multitenancy\Tasks\SwitchRouteCacheTask; | ||
use Spatie\Multitenancy\Tests\TestCase; | ||
|
||
class SwitchRouteCacheTaskTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
config()->set('multitenancy.switch_tenant_tasks', [SwitchRouteCacheTask::class]); | ||
} | ||
|
||
/** @test */ | ||
public function it_will_use_a_different_routes_cache_environment_variable_for_each_tenant() | ||
{ | ||
/** @var \Spatie\Multitenancy\Models\Tenant $tenant */ | ||
$tenant = factory(Tenant::class)->create(); | ||
$tenant->makeCurrent(); | ||
$this->assertEquals("bootstrap/cache/routes-v7-tenant-{$tenant->id}.php", env('APP_ROUTES_CACHE')); | ||
|
||
/** @var \Spatie\Multitenancy\Models\Tenant $anotherTenant */ | ||
$anotherTenant = factory(Tenant::class)->create(); | ||
$anotherTenant->makeCurrent(); | ||
$this->assertEquals("bootstrap/cache/routes-v7-tenant-{$anotherTenant->id}.php", env('APP_ROUTES_CACHE')); | ||
|
||
$tenant->makeCurrent(); | ||
$this->assertEquals("bootstrap/cache/routes-v7-tenant-{$tenant->id}.php", env('APP_ROUTES_CACHE')); | ||
|
||
$anotherTenant->makeCurrent(); | ||
$this->assertEquals("bootstrap/cache/routes-v7-tenant-{$anotherTenant->id}.php", env('APP_ROUTES_CACHE')); | ||
|
||
Tenant::forgetCurrent(); | ||
$this->assertNull(env('APP_ROUTES_CACHE')); | ||
} | ||
} |