简体   繁体   English

在Laravel 4中将尾部斜杠附加到url

[英]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? 如何在Laravel 4中添加尾部斜杠到url?

EDIT: As timgws mentioned, this solution no longer works in Laravel 4.1+ 编辑:正如timgws所说,这个解决方案不再适用于Laravel 4.1+

If you comment out line 16 in bootstrap/start.php 如果你在bootstrap / start.php中注释掉第16行

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

//$app->redirectIfTrailingSlash();

It should no longer redirect to the URL without the slash. 它不应该在没有斜杠的情况下重定向到URL。

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 ) 如果您使用的是Laravel 4.0( 而非 4.1),则应在bootstrap / start.php中注释掉第16行( 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!). Laravel 4.1删除了redirectIfTrailingSlash因为它现在可以在.htaccess文件中控制(因为它始终应该是!)。

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. 您还可能还想更改vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php 以便生成的URL在它们的末尾有一个斜杠。

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! 更改此代码后,使用URL生成器的模板和代码(例如URL::to() )现在将生成URL末尾带有斜杠的URL,这样可以节省模板中的额外字符!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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