Laravel 9.3基于请求数据识别的多租户搭建疑问:仅替换路由中间件即可实现?
Absolutely, swapping the middleware is a core step for setting up request-data-based tenancy with Tenancy for Laravel v3, but there are a few extra configuration pieces you’ll need to get everything working smoothly. Let’s break down the full process:
1. Finish the Quickstart Foundation First
Before tweaking the middleware, make sure you’ve completed the basic quickstart setup:
- Created or configured your Tenant model (you can use the default
Stancl\Tenancy\Tenantor a custom one) - Ran all required tenancy migrations
- Set up central and tenant database connections
- Configured core tenancy settings in
config/tenancy.php
2. Update Your Tenant Routes
Your planned route modification is spot-on—replacing InitializeTenancyByDomain::class with InitializeTenancyByRequestData::class is exactly what you need to do for request-based identification. Here’s your code for clear reference:
Before Modification:
<?php declare(strict_types=1); use Illuminate\Support\Facades\Route; use Stancl\Tenancy\Middleware\InitializeTenancyByDomain; use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains; /* |-------------------------------------------------------------------------- | Tenant Routes |-------------------------------------------------------------------------- | | Here you can register the tenant routes for your application. | These routes are loaded by the TenantRouteServiceProvider. | | Feel free to customize them however you want. Good luck! | */ Route::middleware([ 'web', InitializeTenancyByDomain::class, PreventAccessFromCentralDomains::class, ])->group(function () { Route::get('/', function () { return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id'); }); });
After Modification:
<?php declare(strict_types=1); use Illuminate\Support\Facades\Route; use Stancl\Tenancy\Middleware\InitializeTenancyByRequestData; use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains; /* |-------------------------------------------------------------------------- | Tenant Routes |-------------------------------------------------------------------------- | | Here you can register the tenant routes for your application. | These routes are loaded by the TenantRouteServiceProvider. | | Feel free to customize them however you want. Good luck! | */ Route::middleware([ 'web', InitializeTenancyByRequestData::class, PreventAccessFromCentralDomains::class, ])->group(function () { Route::get('/', function () { return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id'); }); });
3. Configure Request Data Identification Rules
The critical missing step is telling the middleware where to find tenant identifiers in incoming requests. Open config/tenancy.php, locate the tenant_identification section, and update the request_data array to define your preferred sources (query params, headers, request body, etc.):
'tenant_identification' => [ // ... other identification methods (you can keep or remove unused ones) 'request_data' => [ 'sources' => [ // Pick one or more sources that fit your use case 'query' => 'tenant_id', // e.g., access via ?tenant_id=123 'header' => 'X-Tenant-ID', // e.g., send X-Tenant-ID: 123 in request headers // 'body' => 'tenant_id', // For POST/PUT requests with form/JSON payloads ], 'model' => \App\Models\Tenant::class, // Your tenant model class 'column' => 'id', // The column in your tenants table to match against ], ],
4. Test the Setup
Once configured, verify it works by sending requests with your chosen tenant identifier:
- For query params:
GET http://your-app-url/?tenant_id=1 - For headers: Send a request with the
X-Tenant-ID: 1header included
You should see the correct tenant ID returned in the response, confirming the middleware successfully identified the tenant.
Key Notes to Remember
- Keep the
PreventAccessFromCentralDomainsmiddleware: It blocks access to tenant routes from your application’s central domain(s), which is a critical security measure. - Ensure your Tenant model implements
Stancl\Tenancy\Contracts\TenantContract(or extend the defaultStancl\Tenancy\Tenantmodel to avoid extra work). - If using request body sources, make sure your routes accept the right HTTP methods and parse the body correctly (e.g., set
Content-Type: application/jsonfor JSON payloads).
内容的提问来源于stack exchange,提问作者max123

