Laravel 基于域的路由

Posted

技术标签:

【中文标题】Laravel 基于域的路由【英文标题】:Laravel routing based on domains 【发布时间】:2020-03-07 10:24:30 【问题描述】:

我正在使用(示例)主域:www.maindomain.com 和几个基于 url 前缀的路由组重新开发一个核心 php 网络应用程序到 Laravel 5.8 应用程序:

www.maindomain.com/user
www.maindomain.com/admin
www.maindomain.com/teacher

该应用还有大约 50 个插件域,在旧应用中,它们都指向一个位置(例如:public_html/addondomains),显示的内容由 $_SERVER['http_host' ]。

我想在 Laravel 中做同样的事情,但我无法为插件域设置路由规则。因此,它需要将任何不是 maindomain.com 的 url 路由到控制器函数。有什么帮助吗? :)

【问题讨论】:

【参考方案1】:

你可以使用 Route::domain()

例子:

//http://www.maindomain.com/test
    Route::domain('www.maindomain.com')->group(function () 
        Route::get('/test', 'HomeController@test')->name('main.test');
    );

//http://example.maindomain.com/test
    Route::domain('addon.maindomain.com')->group(function () 
        Route::get('/test', 'AddonController@test')->name('addon.test');
    );

https://laravel.com/docs/5.8/routing#route-group-sub-domain-routing

确保您的服务器配置为通过单个 Laravel 安装为这些域提供服务。

【讨论】:

以上是关于Laravel 基于域的路由的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 4 响应标头:拒绝基于源域的 ajax 请求

Laravel 5.2 路由仅适用于根目录

Laravel框架下路由的使用(源码解析)

无法基于角色 Laravel 8 保护路由

laravel 路由重定向问题(laravel 5.3)

Laravel 大将之路由模块