Laravel:如何使路径返回网址带有斜杠? [英] Laravel: How do I make route return urls with trailing slashes?

查看:235
本文介绍了Laravel:如何使路径返回网址带有斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使Laravel 5.3创建带有斜杠的路由URL?

How do I make Laravel 5.3 create route URLs with trailing slashes?

该路线定义为

 Route::get('/home/', ['as' => 'home', 'uses' => 'HomeController@index']);

然后我在视图中使用route helper函数创建URL:

I then create the url with the route helper function in a view:

{{ route('home') }}

这将创建http://localhost:8000/home而不是http://localhost:8000/home/.

此问题与 Laravel-在路由末尾添加斜线,但是这个问题也没有答案,而且在我看来,我的描述短了一点.

This question is very similar to Laravel - append a trailing slash in routes, but that question doesn't have an answer either and it seems to me that my description is a tat shorter.

文档中的两种可能的命名方法没有区别:

The two possible naming methods from the docs don't make a difference:

Route::get('/home/', ['as' => 'home', 'uses' => 'HomeController@index']);
Route::get('/home/', 'HomeController@index')->name('home');

推荐答案

因为Laravel会在URL末尾删除斜杠,因此您只需使用{{ route('home') }}/即可.

Because Laravel will remove slash at the end of URL so you just can do it by using {{ route('home') }}/.

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

这篇关于Laravel:如何使路径返回网址带有斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆