Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: How do I make route helper return urls with trailing slashes?

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');
like image 799
tback Avatar asked Dec 06 '16 08:12

tback


People also ask

How do you add trailing slash to URL in Laravel?

Laravel includes a block of code in its . htaccess file that redirects any url with trailing slash to non slashed url by default if the uri is not pointing to a directory. You can remove that piece of code and it should work fine in case you've appended a slash to each laravel route manually.

Should I add trailing slash to URL?

Historically, a trailing slash marked a directory and a URL without a trailing slash at the end used to mean that the URL was a file. Today, however, trailing slashes are purely conventional, and Google does not care whether you use them; as long as you're consistent.

What is a trailing slash in URL?

Email Subscription. A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.

What is the difference between route and URL in Laravel?

For a start it is using a named route. This means that the link between the form and its controller are not dictated by the url that the user sees. The next advantage is that you can pass additional parameters into the route helper and it will put those in the correct place in the form action.


1 Answers

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.

like image 114
UKRman Avatar answered Oct 16 '22 22:10

UKRman