Append trailing slash to url in Laravel 4

2020-04-11 04:18发布

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

How to append trailing slash to url in Laravel 4?

2条回答
Anthone
2楼-- · 2020-04-11 04:36

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
查看更多
别忘想泡老子
3楼-- · 2020-04-11 04:39

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!

查看更多
登录 后发表回答