2016-12-06 70 views
8

如何讓Laravel 5.3創建帶有斜線的路由URL?Laravel:我如何讓路線路線返回帶有斜線的路線?

路線被定義爲

Route::get('/home/', ['as' => 'home', 'uses' => '[email protected]']); 

然後創建與在視圖中的路線輔助函數的網址:

{{ route('home') }} 

這產生http://localhost:8000/home代替http://localhost:8000/home/

此問題與Laravel - append a trailing slash in routes非常相似,但該問題也沒有答案,在我看來,我的描述更短。

編輯: 從文檔的兩種可能的命名方法不有所作爲:

Route::get('/home/', ['as' => 'home', 'uses' => '[email protected]']); 
Route::get('/home/', '[email protected]')->name('home'); 
+0

當你使用尾部斜線訪問它時會發生什麼? afaik它會重定向到別的地方。其解決方案之一是[這麼回答](http://stackoverflow.com/a/21735548/4648586) –

+0

只要Web服務器不重定向laravel將響應請求。 – tback

+0

它與['.htaccess'](https://github.com/laravel/laravel/blob/master/public/.htaccess#L10)有關。正如它在[這個問題](http://stackoverflow.com/questions/34441338/laravel-5-bad-behaviour-while-removing-trailing-slash)中討論的,你必須在公共目錄中編輯你的'.htaccess'。雖然... [在某些討論中](https://github.com/laravel/framework/issues/58#issuecomment-12367008) –

回答

1

由於Laravel將在網址的結尾處,刪除斜槓所以你纔可以使用{{ route('home') }}/做到這一點。

參考:https://github.com/laravel/framework/blob/5.3/src/Illuminate/Routing/UrlGenerator.php#L308-L345

+0

感謝鏈接@桑阮。我必須等待幾天,直到它不再受傷,然後我會接受你的答案。 – tback

+0

或創建你自己的[helper method](http://laravel.io/forum/02-03-2015-best-practices-for-custom-helpers-on-laravel-5?page=1) –