mirror of
https://github.com/stancl/tenancy-docs.git
synced 2025-12-12 02:04:03 +00:00
Merge pull request #271 from stancl/l11-update
Update docs for Laravel 11
This commit is contained in:
commit
3f60cdbe58
3 changed files with 23 additions and 64 deletions
|
|
@ -18,10 +18,12 @@ First, enable the `UniversalRoutes` feature by uncommenting the following line i
|
|||
Stancl\Tenancy\Features\UniversalRoutes::class,
|
||||
```
|
||||
|
||||
Next, go to your `app/Http/Kernel.php` file and add the following middleware group:
|
||||
Next, go to your `bootstrap/app.php` file and add the following middleware group:
|
||||
|
||||
```php
|
||||
'universal' => [],
|
||||
->withMiddleware(function (Middleware $middleware) {
|
||||
$middleware->group('universal', []);
|
||||
})
|
||||
```
|
||||
|
||||
We will use this middleware group as a "flag" on the route, to mark it as a universal route. We don't need any actual middleware inside the group.
|
||||
|
|
|
|||
|
|
@ -6,9 +6,7 @@ section: content
|
|||
|
||||
# Installation {#installation}
|
||||
|
||||
> Note: Laravel 11 has a different folder structure than Laravel 10. An updated installation guide is coming soon. For now you can see [this discussion](https://github.com/archtechx/tenancy/pull/1180).
|
||||
|
||||
Laravel 6.0 or higher is needed.
|
||||
Laravel 9.0 or higher is needed.
|
||||
|
||||
Require the package using composer:
|
||||
|
||||
|
|
@ -29,18 +27,13 @@ It will create:
|
|||
- a routes file (`routes/tenant.php`),
|
||||
- and a service provider file `app/Providers/TenancyServiceProvider.php`
|
||||
|
||||
Then add the service provider to your `config/app.php` file:
|
||||
Then add the service provider to your `bootstrap/providers.php` file:
|
||||
|
||||
```php
|
||||
/*
|
||||
* Application Service Providers...
|
||||
*/
|
||||
App\Providers\AppServiceProvider::class,
|
||||
App\Providers\AuthServiceProvider::class,
|
||||
// App\Providers\BroadcastServiceProvider::class,
|
||||
App\Providers\EventServiceProvider::class,
|
||||
App\Providers\RouteServiceProvider::class,
|
||||
App\Providers\TenancyServiceProvider::class, // <-- here
|
||||
return [
|
||||
App\Providers\AppServiceProvider::class,
|
||||
App\Providers\TenancyServiceProvider::class, // <-- here
|
||||
];
|
||||
```
|
||||
|
||||
And finally, if you want to use a different central database than the one defined by `DB_CONNECTION` in the file `.env`, name your central connection (in `config/database.php`) `central` — or however else you want, but make sure it's the same name as the `tenancy.central_connection` config.
|
||||
|
|
|
|||
|
|
@ -12,8 +12,6 @@ We recommend following this tutorial just **to get things working** so that you
|
|||
|
||||
## Installation {#installation}
|
||||
|
||||
> Note: Laravel 11 has a different folder structure than Laravel 10. An updated installation guide is coming soon. For now you can see [this discussion](https://github.com/archtechx/tenancy/pull/1180).
|
||||
|
||||
First, require the package using composer:
|
||||
|
||||
```php
|
||||
|
|
@ -34,18 +32,13 @@ Let's run the migrations:
|
|||
php artisan migrate
|
||||
```
|
||||
|
||||
Register the service provider in `config/app.php`. Make sure it's on the same position as in the code snippet below:
|
||||
Register the service provider in `bootstrap/providers.php`:
|
||||
|
||||
```php
|
||||
/*
|
||||
* Application Service Providers...
|
||||
*/
|
||||
App\Providers\AppServiceProvider::class,
|
||||
App\Providers\AuthServiceProvider::class,
|
||||
// App\Providers\BroadcastServiceProvider::class,
|
||||
App\Providers\EventServiceProvider::class,
|
||||
App\Providers\RouteServiceProvider::class,
|
||||
App\Providers\TenancyServiceProvider::class, // <-- here
|
||||
return [
|
||||
App\Providers\AppServiceProvider::class,
|
||||
App\Providers\TenancyServiceProvider::class, // <-- here
|
||||
];
|
||||
```
|
||||
|
||||
## Creating a tenant model {#creating-a-tenant-model}
|
||||
|
|
@ -84,50 +77,20 @@ In other words, it creates & migrates the tenant's database after he's created
|
|||
|
||||
## Central routes {#central-routes}
|
||||
|
||||
We'll make a small change to the `app/Providers/RouteServiceProvider.php` file. Specifically, we'll make sure that central routes are registered on central domains only.
|
||||
We'll make a small change to your existing route files. Specifically, we'll make sure that central routes are registered on central domains only:
|
||||
|
||||
```php
|
||||
protected function mapWebRoutes()
|
||||
{
|
||||
foreach ($this->centralDomains() as $domain) {
|
||||
Route::middleware('web')
|
||||
->domain($domain)
|
||||
->namespace($this->namespace)
|
||||
->group(base_path('routes/web.php'));
|
||||
}
|
||||
}
|
||||
// routes/web.php, api.php or any other central route files you have
|
||||
|
||||
protected function mapApiRoutes()
|
||||
{
|
||||
foreach ($this->centralDomains() as $domain) {
|
||||
Route::prefix('api')
|
||||
->domain($domain)
|
||||
->middleware('api')
|
||||
->namespace($this->namespace)
|
||||
->group(base_path('routes/api.php'));
|
||||
}
|
||||
}
|
||||
|
||||
protected function centralDomains(): array
|
||||
{
|
||||
return config('tenancy.central_domains');
|
||||
}
|
||||
```
|
||||
|
||||
Call these methods manually from your `RouteServiceProvider`'s `boot()` method, instead of the `$this->routes()` calls.
|
||||
|
||||
```php
|
||||
public function boot()
|
||||
{
|
||||
$this->configureRateLimiting();
|
||||
|
||||
$this->routes(function () {
|
||||
$this->mapApiRoutes();
|
||||
$this->mapWebRoutes();
|
||||
foreach (config('tenancy.central_domains') as $domain) {
|
||||
Route::domain($domain)->group(function () {
|
||||
// your actual routes
|
||||
});
|
||||
}
|
||||
```
|
||||
|
||||
Alternatively, to keep your route files more clean, you can use [this approach](https://github.com/archtechx/tenancy/pull/1180#issuecomment-2006098346) to register all of your routes in the `using` callback of the Application Builder.
|
||||
|
||||
## Central domains {#central-domains}
|
||||
|
||||
Now we need to actually specify the central domains. A central domain is a domain that serves your "central app" content, e.g. the landing page where tenants sign up. Open the `config/tenancy.php` file and add them in:
|
||||
|
|
@ -146,6 +109,7 @@ If you're using Laravel Sail, no changes are needed, default values are good to
|
|||
'localhost',
|
||||
],
|
||||
```
|
||||
|
||||
## Tenant routes {#tenant-routes}
|
||||
|
||||
Your tenant routes will look like this by default:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue