我在 laravel 4.2 应用程序中使用子域路由。这是我的路线文件:
Route::group(array('domain' => '{keyword}.example.com'), function() {
Route::get('/', 'FrontendHomeController@index');
});
Route::get('/', ['uses' => 'FrontendHomeController@index', 'as' => 'home']);
Route::get('hotels', ['uses' => 'FrontendHotelController@index', 'as' => 'hotelsearch']);
Route::get('hotel/{slug}/{id}', ['uses' => 'FrontendHotelController@detail', 'as' => 'hoteldetail']);
[...]
所以我有几个页面使用子域,例如 keyword.example.com
、another.example.com
。但是大多数其他页面都是常规的 example.com/page
URL。到目前为止效果很好。我使用 Laravel url/route helpers 生成导航链接,例如{{ url('hotels') }}
或 {{ route('hotelsearch') }}
。
现在,在子域页面上,生成的 URL 包含子域。例如。在 keyword.example.com 上,{{ url('hotels') }}
生成 keyword.example.com/hotels
而不是 example.com/hotels
。
我想删除使用 url()
或 route()
助手生成的所有链接的子域,这些助手应始终指向根域。
是否有参数,或者我是否必须以某种方式覆盖辅助方法?
最佳答案
模板中的url
函数包裹了Illuminate\Routing\UrlGenerator::to
,它不允许你指定域,但你可以使用相同的Route::group
technique来封装所有主域路由。 (它不需要通配符,如文档中所示。)
Route::group(array('domain' => 'www.example.com'), function() {
Route::get('hotels', ['uses' => 'FrontendHotelController@index', 'as' => 'hotelsearch']);
// ...
});
关于php - 使用 Laravel 4 从 URL 中删除子域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27569750/