Append trailing slash to url in Laravel 4

后端 未结 2 1059
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-20 04:20

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

How to append trailing slash to url in Laravel 4?

相关标签:
2条回答
  • 2021-01-20 04:48

    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
    
    0 讨论(0)
  • 2021-01-20 04:58

    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!

    0 讨论(0)
提交回复
热议问题