2016-12-06 95 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) –