11

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

The route is defined as

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

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

{{ route('home') }}

This creates http://localhost:8000/home instead of http://localhost:8000/home/.

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.

Edit: 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');
tback
  • 11,138
  • 7
  • 47
  • 71
  • what happen when you access the one with trailing slash? afaik it will redirect to somewhere else. one of its solution is [this so answer](http://stackoverflow.com/a/21735548/4648586) – Bagus Tesa Dec 06 '16 at 08:56
  • As long as the webserver doesn't redirect laravel will respond to the request. – tback Dec 06 '16 at 09:22
  • it something had to do with the [`.htaccess`](https://github.com/laravel/laravel/blob/master/public/.htaccess#L10). as it discussed in [this question](http://stackoverflow.com/questions/34441338/laravel-5-bad-behaviour-while-removing-trailing-slash) you had to edit your `.htaccess` in public directory. although... [in a certain discussion](https://github.com/laravel/framework/issues/58#issuecomment-12367008) – Bagus Tesa Dec 06 '16 at 09:22
  • 1
    I had a deep look at the `UrlGenerator` and I thought of extending it for this purpose, namely changing its `format()` method, which strips the trailing slash. Unfortunately, I realized that Laravel is not flexible enough with it, so doing it in a fancy way is not possible. Laravel even registers some magic on `UrlGenerator` during runtime right after registering the `UrlGenerator` itself. This feature is pretty badly designed, to be honest. So your only chance is to create your own helper (or overwrite existing `route()` helper). – Robo Robok May 30 '21 at 07:49

2 Answers2

1

The marked variant is more similar to a crutch than to the decision as URL is in sitemap.xml and still anywhere therefore in this case, it is better to edit everything only in the directory routes/*.php, That is all same the variant with UrlGenerator census will be the most correct. In this case, there is a very useful package (illuminatech/url-trailing-slash) that essentially should solve this problem. All changes will be only at the routing level.

UKRman
  • 404
  • 3
  • 16
0

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

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

Sang Nguyen
  • 1,702
  • 15
  • 16
  • 1
    or create your own [helper method](http://laravel.io/forum/02-03-2015-best-practices-for-custom-helpers-on-laravel-5?page=1) – Bagus Tesa Dec 06 '16 at 09:29