简体   繁体   中英

Append trailing slash to url in Laravel 4

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

How to append trailing slash to url in Laravel 4?

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM