laravel - Laravel 路由在 2 个环境中与子域一起使用
问题描述
我正在编写一个平台来生成子网站。
我有一条这样的路线,在本地运行良好:
//Website
Route::domain('{slug}.domain.test')->group(function () {
Route::get('/','WebsitesController@show')->name('web_website_show');
});
我希望能够使其在生产(其他领域)中也能正常工作,所以我做到了:
//Website
Route::domain('{slug}.{domain}')->group(function () {
Route::get('/','WebsitesController@show')->name('web_website_show');
});
在我的模板中:
<a href="{{ route('web_website_show',['slug' => $website->slug, 'domain' => Request::getHost() ]) }}">Website</a>
生成的 URL 看起来很神奇,但路由不起作用并将我带到主域的父页面。
我做错了什么?
谢谢
解决方案
在 Laravel 中使用这样的域路由有点痛苦。
最近在一个应用程序中,我从应用程序 URL 中解析了域部分,然后将其设置为配置值,如下所示:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
config([
'app.domain' => parse_url(config('app.url'), PHP_URL_HOST),
]);
}
}
然后,您可以在域路由中使用它:
Route::domain('{slug}.'.config('app.domain'), function () {
// Subdomain routes that work in all environments
});
推荐阅读
- javascript - 谷歌翻译小部件延迟显示在网页上
- amazon-s3 - 将名称包含斜杠的文件上传到 AWS S3 存储桶
- sql - 如何插入多个insert sql语句
- kotlin - 如何在 Kotlin Multiplatform 中将默认的 `jvm` sourceSet 添加到 `android` 目标?
- swift - 如何获取已编辑的 NSOutlineView 文本字段单元格的“项目”或行索引?
- python - 避免数据帧 groupby 结果变成系列
- youtube-iframe-api - 如何在 youtube api 嵌入代码中删除更多视频 div
- php - dropzone 传递空文件名
- angular - Angular 将下拉选择选项设置为迭代前的默认选择
- apache-camel - 如何读取文件并作为流发送到另一个端点