You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Sail + TenancyForLaravel本地子域名无法访问问题排查

问题诊断与解决方案

核心问题分析

当前配置仅为中心域名(localhost、127.0.0.1)注册了路由,完全缺失租户子域名的路由映射逻辑;同时本地DNS解析未配置子域名指向,导致foo.localhost/bar.localhost无法被正确识别。


分步修复步骤

1. 补全租户路由映射配置

修改RouteServiceProvider.php,添加租户路由的注册逻辑:

<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    public const HOME = '/home';

    public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            $this->mapApiRoutes();
            $this->mapWebRoutes();
            // 添加租户路由映射
            $this->mapTenantWebRoutes();
            $this->mapTenantApiRoutes();
        });
    }

    protected function configureRateLimiting()
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });
    }

    protected function mapWebRoutes()
    {
        foreach ($this->centralDomains() as $domain) {
            Route::middleware('web')
                ->domain($domain)
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));
        }
    }

    protected function mapApiRoutes()
    {
        foreach ($this->centralDomains() as $domain) {
            Route::prefix('api')
                ->domain($domain)
                ->middleware('api')
                ->namespace($this->namespace)
                ->group(base_path('routes/api.php'));
        }
    }

    // 新增租户Web路由注册
    protected function mapTenantWebRoutes()
    {
        Route::middleware(['web', 'tenancy'])
            ->namespace($this->namespace)
            ->group(base_path('routes/tenant/web.php'));
    }

    // 新增租户API路由注册
    protected function mapTenantApiRoutes()
    {
        Route::prefix('api')
            ->middleware(['api', 'tenancy'])
            ->namespace($this->namespace)
            ->group(base_path('routes/tenant/api.php'));
    }

    protected function centralDomains(): array
    {
        return config('tenancy.central_domains');
    }
}

2. 创建租户路由文件

在项目根目录下创建routes/tenant目录,并添加对应的路由文件:

routes/tenant/web.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return '租户 ' . tenant('id') . ' 主页';
});

routes/tenant/api.php

<?php

use Illuminate\Support\Facades\Route;

Route::get('/info', function () {
    return response()->json([
        'tenant_id' => tenant('id'),
        'domain' => request()->getHost()
    ]);
});

3. 配置本地DNS解析

编辑本地hosts文件,将子域名指向127.0.0.1:

  • Windows:C:\Windows\System32\drivers\etc\hosts
  • Linux/macOS:/etc/hosts

添加以下两行:

127.0.0.1 foo.localhost
127.0.0.1 bar.localhost

4. 确保租户已创建并关联域名

通过Artisan命令创建对应租户并绑定域名:

sail artisan tenancy:create foo --domain=foo.localhost
sail artisan tenancy:create bar --domain=bar.localhost

5. 重启Sail服务

sail down && sail up -d

验证

访问http://foo.localhost和http://bar.localhost,应该能看到对应租户的主页内容;访问http://foo.localhost/api/info可获取租户API响应。

内容的提问来源于stack exchange,提问作者Tufan Çağkan Sökmen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 16:35:25