使用Laravel 4从URL中删除子域名

4

我使用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.comanother.example.com。但是大多数页面都是常规的example.com/page URL。目前这样做没问题。我使用Laravel url/route助手生成导航链接,例如{{ url('hotels') }}{{ route('hotelsearch') }}

现在,在子域页面上,生成的URL包括子域。例如,在keyword.example.com上,{{ url('hotels') }}会生成keyword.example.com/hotels而不是example.com/hotels

我想要移除所有用url()route()助手生成的链接的子域,这些助手应该始终指向根域名。

是否有参数或者我必须以某种方式重写助手方法?

1个回答

1
在模板中,url函数封装了Illuminate\Routing\UrlGenerator::to,它不允许您指定域名,但是您可以使用相同的Route::group技术来封装所有主域路由。(文档中所示,它不需要通配符。)
Route::group(array('domain' => 'www.example.com'), function() {
    Route::get('hotels', ['uses' => 'FrontendHotelController@index', 'as' => 'hotelsearch']);
    // ...
});

1
这使子域名URL无效,这是好的。但它并不能解决“url”的问题。然而,它可以使用命名路由(和“route”助手)解决! - aebersold
这实际上运行得非常完美,生成基础域名URL。 - Igor Skoric

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接