【问题标题】:laravel route grouping by with dynamic subdomain and without subdomainlaravel 路由分组方式,带动态子域和不带子域
【发布时间】:2019-11-08 07:36:03
【问题描述】:

我在 hyn 多租户 (https://laravel-tenancy.com/) 的支持下开发一个 laravel 多租户应用程序, 如果 url 是“http://domain.test”,我只想显示我的主页,如果 url 是 laravel5.6 中的“http://tenant.domain.test”,则显示用户(租户-每个用户的动态名称)主页

我试过了

//enter to this group if subdomain is present and show user homepage

    Route::domain('{tenant}.domain.test')->group(function () {
        Route::get('/', 'HomePage');
        Auth::routes();
    });

//else show main homepage

    Route::domain('domain.test')->group(function () {
        Route::get('/', 'HomePage');
    });

但问题是这需要在每个视图中传递 {subdomain} 的值,否则会出现类似

的错误

缺少 [Route: login] [URI: login] 的必需参数。

【问题讨论】:

    标签: laravel multi-tenant laravel-5.6


    【解决方案1】:
    Route::domain(checkDomain())->group(function () {
        Route::get('/', function () {
            return "You are on a custom domain";
        });
    });
    
    Route::get('/', function () {
         return "You are on main domain.";
    });
    
    public function checkDomain()
    {
        if (request()->getHttpHost() == 'domain.test') {
            return request()->getHttpHost();
        }
    }
    

    【讨论】:

    • 我期待一个通配符子域路由,例如 if (request()->getHttpHost() == '*.domain.test') 谢谢
    • 在你的建议中 Route::domain(checkDomain())->group(function () { Route::get('/', function () { return "你在一个自定义域上" ; }); }); Route::get('/', function () { return "你在主域上。"; }); public function checkDomain() { if (request()->getHttpHost() == '*.domain.test') { return request()->getHttpHost(); } }
    猜你喜欢
    • 2017-10-28
    • 2019-12-23
    • 2021-02-25
    • 2011-03-06
    • 2015-11-17
    • 2017-01-06
    • 2017-01-29
    • 1970-01-01
    • 2018-03-01
    相关资源
    最近更新 更多