Laravel 13 不存在,当前最新稳定版是 Laravel 12,stancl/tenancy v4 支持 Laravel 10–12;central_domains 用于定义管理中心域名,需精确配置于 config/tenancy.php 并在 Nginx 中优先匹配,同时路由须用 Route::domain() 显式绑定。

Laravel 13 并不存在——截至 2026 年 11 月,Laravel 最新稳定版是 Laravel 12,而 stancl/tenancy v4 明确支持 Laravel 10–12。你实际使用的应是 Laravel 12 + tenancy v4,配置方式与官方文档一致。
central_domains 是用于定义「管理中心」访问入口的域名列表,它和租户子域名(如 tenant1.example.com)完全隔离,由 central 路由组接管,不经过租户识别流程。
central_domains 配置位置
在 config/tenancy.php 中找到 'central_domains' 键:
'central_domains' => [
'admin.example.com',
'cms.example.com',
],
✅ 域名必须精确匹配 Host 头(不带协议、不带通配符),例如:
- 正确:
admin.example.com - 错误:
https://admin.example.com、*.example.com、admin.example.com:8000
注意:这个数组只影响路由分发逻辑,不自动注册中间件或路由。你仍需确保这些域名在 Web 服务器(Nginx/Apache)中已正确解析并指向 Laravel 应用。
Web 服务器需单独配置 central 域名
Nginx 示例(与租户泛解析共存):
Laravel Cloud(cloud.laravel.com)。此技能用于处理所有 Laravel Cloud 请求,包括搜索和读取数据。只要任务涉及 Laravel Cloud,即使用此技能。
# 管理中心域名 —— 精确匹配
server {
server_name admin.example.com cms.example.com;
root /var/www/laravel/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 租户子域名 —— 泛解析(保留原有配置)
server {
server_name ~^(?<tenant>.+)\.example\.com$;
# ... 同上,但需传递 $tenant 变量(如 fastcgi_param HTTP_X_TENANT $tenant;)
}</tenant>
⚠️ 关键点:central_domains 的域名不能被租户泛解析规则捕获,否则请求会误入 tenant 上下文,导致数据库连接失败或 404。
central 路由必须显式绑定 domain
在 routes/web.php(或其他 central 路由文件)中,用 Route::domain() 包裹管理中心路由:
// routes/web.php
use Illuminate\Support\Facades\Route;
foreach (config('tenancy.central_domains') as $domain) {
Route::domain($domain)->group(function () {
// 这里写后台管理、租户创建、系统设置等 central 功能
Route::get('/', function () {
return view('admin.dashboard');
})->name('admin.home');
Route::get('/tenants', [\App\Http\Controllers\TenantController::class, 'index']);
});
}
✅ 这样才能确保只有访问 admin.example.com/tenants 时才进入 central 控制台,且不会触发 tenant 初始化逻辑。
常见问题排查
访问
admin.example.com却跳转到租户页面?
→ 检查 Nginxserver_name顺序,central 域名规则必须排在租户泛解析之前(Nginx 优先匹配第一个匹配项)。页面报错 “Tenant not resolved” 或 “Database connection [tenant] not configured”?
→ 说明请求进入了 tenant 生命周期,确认Route::domain(...)是否漏写,或central_domains配置有拼写错误(比如多了一个空格或大小写不一致)。新增 central 域名后不生效?
→ 清除 Laravel 路由缓存:php artisan route:clear(开发环境可关掉缓存便于调试)。
不复杂但容易忽略。










