mirror of
https://github.com/stancl/tenancy-docs.git
synced 2025-12-12 10:14:03 +00:00
Merge branch 'master' of github.com:stancl/tenancy-docs
This commit is contained in:
commit
48be7f382b
6 changed files with 41 additions and 7 deletions
|
|
@ -17,6 +17,8 @@ Routes in the `routes/web.php` file are the central routes. When they are visite
|
|||
|
||||
However, since you don't want routes related to the app on your main domain and sign up forms on tenant domains, you must also define what domains host the central stuff in the `tenancy.exempt_domains` config.
|
||||
|
||||
The exempt domains should not include the protocol. For example, you should include `example.com` rather than `https://example.com`.
|
||||
|
||||
## Using central things inside the tenant app {#using-central-things-inside-the-tenant-app}
|
||||
|
||||
To use central things (databases/caches/Redis connections/filesystems/...) on special places of your tenant app, you may do the following.
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ Controller namespace used for routes in `routes/tenant.php`. The default value i
|
|||
|
||||
### `exempt_domains` {#exempt-domains}
|
||||
|
||||
If a hostname from this array is visited, the `tenant.php` routes won't be registered, letting you use the same routes as in that file.
|
||||
If a hostname from this array is visited, the `tenant.php` routes won't be registered, letting you use the same routes as in that file. This should be the domain without the protocol (i.e., `example.com` rather than `https://example.com`).
|
||||
|
||||
### `database` {#database}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ Tenant::new()->withData([
|
|||
|
||||
// Make tweaks to the connection before bootstrapping tenancy
|
||||
tenancy()->hook('bootstrapping', function ($tenantManager) {
|
||||
config(['database.connections.someTenantConnectionTemplate.name' => $tenantManager->tenant['database_name']]);
|
||||
config(['database.connections.someTenantConnectionTemplate.password' => $tenantManager->tenant['database_password']]);
|
||||
config(['database.connections.someTenantConnectionTemplate.host' => $tenantManager->tenant['database_host']]);
|
||||
config(['database.connections.someTenantConnectionTemplate.name' => $tenantManager->getTenant('database_name')]);
|
||||
config(['database.connections.someTenantConnectionTemplate.password' => $tenantManager->getTenant('database_password')]);
|
||||
config(['database.connections.someTenantConnectionTemplate.host' => $tenantManager->getTenant('database_host')]);
|
||||
});
|
||||
```
|
||||
|
|
|
|||
|
|
@ -60,4 +60,10 @@ Storage::disk('public')->put($filename, $data);
|
|||
Storage::disk('local')->put("public/$filename", $data);
|
||||
```
|
||||
|
||||
Note that every request for a tenant asset requires a full framework boot and tenancy initialization. This is not ideal if you have some assets that occur on each page (like logos). So for non-private assets, you may want to create a disk and use URLs from that disk instead. For example:
|
||||
|
||||
```php
|
||||
Storage::disk('app-public')->url('tenants/logos/' . tenant()->id . '.png');
|
||||
```
|
||||
|
||||
If you want to store something globally, simply create a new disk and *don't* add it to the `tenancy.filesystem.disks` config.
|
||||
|
|
|
|||
|
|
@ -7,14 +7,40 @@ section: content
|
|||
|
||||
# Middleware Configuration {#middleware-configuration}
|
||||
|
||||
## Header or query parameter based identification {#header-or-query-parameter-based-identification}
|
||||
|
||||
To identify tenants using request headers or query parameters, you may use the `InitializeTenancyByRequestData` middleware.
|
||||
|
||||
Create a **central** route (don't apply the `tenancy` middleware group on it and don't put it into `routes/tenant.php`) and apply this middleware on the route:
|
||||
|
||||
```php
|
||||
\Stancl\Tenancy\Middleware\InitializeTenancyByRequestData::class
|
||||
```
|
||||
|
||||
To customize the header, query parameter, and `onFail` logic, you may do this in your `AppServiceProvider::boot()`:
|
||||
|
||||
```php
|
||||
// use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData::class;
|
||||
|
||||
$this->app->bind(InitializeTenancyByRequestData::class, function ($app) {
|
||||
return new InitializeTenancyByRequestData('header name', 'query parameter', function ($exception) {
|
||||
// return redirect()->route('foo');
|
||||
});
|
||||
});
|
||||
```
|
||||
|
||||
To disable identification using header or query parameter, set the respective parameter to `null`.
|
||||
|
||||
## Customizing the onFail logic {#customizing-the-onfail-logic}
|
||||
|
||||
When a tenant route is visited and 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;
|
||||
|
||||
$this->app->bind(InitializeTenancy::class, function ($app) {
|
||||
return new InitializeTenancy(function ($exception) {
|
||||
return new InitializeTenancy(function ($exception, $request, $next) {
|
||||
// return redirect()->route('foo');
|
||||
});
|
||||
});
|
||||
```
|
||||
```
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue