> For the complete documentation index, see [llms.txt](https://senselab.gitbook.io/senselab-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://senselab.gitbook.io/senselab-docs/vendor/spatie/laravel-multitenancy/docs/using-tasks-to-prepare-the-environment/switching-route-cache-paths.md).

# Switching route cache paths

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.

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
],
```

### A route cache for each tenant

In the default scenario, all tenants have different routes. The package creates a route cache file for each tenant: `bootstrap/cache/routes-v7-tenant-{$tenant->id}.php`.

**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.

### Route cache shared across the tenants

It's the scenario where all tenants use the same routes. The package creates a shared route cache file for all tenants: `bootstrap/cache/routes-v7-tenants.php`.

To enable the feature you should set to `true` the `shared_routes_cache` section of the `multitenancy` config file.

```php
// in config/multitenancy.php

'shared_routes_cache' => true,
```

**Most importantly**, you should use `php artisan tenant:artisan route:cache --tenant=YOUR-TENANT-ID` 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.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://senselab.gitbook.io/senselab-docs/vendor/spatie/laravel-multitenancy/docs/using-tasks-to-prepare-the-environment/switching-route-cache-paths.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
