Skip to content

Commit

Permalink
Merge pull request #308 from spatie/add-route-cache-path-switcher
Browse files Browse the repository at this point in the history
Add route cache path switcher
  • Loading branch information
freekmurze authored Nov 26, 2021
2 parents e7a8c86 + 5bf970c commit e33702c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage
.phpunit.result.cache
.idea
.php-cs-fixer.cache
.php_cs.cache
4 changes: 3 additions & 1 deletion config/multitenancy.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
* A valid task is any class that implements Spatie\Multitenancy\Tasks\SwitchTenantTask
*/
'switch_tenant_tasks' => [
// add tasks here
// \Spatie\Multitenancy\Tasks\PrefixCacheTask::class,
// \Spatie\Multitenancy\Tasks\SwitchTenantDatabaseTask::class,
// \Spatie\Multitenancy\Tasks\SwitchRouteCacheTask::class,
],

/*
Expand Down
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.
19 changes: 19 additions & 0 deletions src/Tasks/SwitchRouteCacheTask.php
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');
}
}
40 changes: 40 additions & 0 deletions tests/Feature/Tasks/SwitchRouteCacheTaskTest.php
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'));
}
}

0 comments on commit e33702c

Please sign in to comment.