Append trailing slash to url in Laravel 4

匿名 (未验证) 提交于 2019-12-03 02:31:01

问题:

Simple question, but I can't find any answer that works:

How to append trailing slash to url in Laravel 4?

回答1:

EDIT: As timgws mentioned, this solution no longer works in Laravel 4.1+

If you comment out line 16 in bootstrap/start.php

https://github.com/laravel/laravel/blob/master/bootstrap/start.php#L16

//$app->redirectIfTrailingSlash(); 

It should no longer redirect to the URL without the slash.

Then you can redo your route to show the trailing slash like:

Route::get('login/', function() { // etc 


回答2:

If you are using Laravel 4.0 (not 4.1), you should comment out line 16 in bootstrap/start.php (https://github.com/laravel/laravel/blob/master/bootstrap/start.php#L16)

//$app->redirectIfTrailingSlash(); 

Laravel 4.1 removed redirectIfTrailingSlash as it can now be controlled in the .htaccess file (as it always should have been!).

You also might also want to change vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php so that generated URLs have a slash at the end of them.

public function to($path, $parameters = array(), $secure = null) { // ...      return trim($root.'/'.trim($path.'/'.$tail, '/'), '/') . '/'; } 

After changing this code, templates and code that use URL generators (such as URL::to()) will now generate URLs with a trailing slash at the end of the URL, which saves you an extra character in your templates!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!