Laravel - Add trailing slash with Redirect::route()

浪子不回头ぞ 提交于 2019-12-07 05:23:59

问题


I'm trying to add a slash to the end of the URL after I use Redirect::route() with Laravel. I've tried numerous examples but couldnt find an answer.

This is what I have so far:

routes.php:

Route::get('/', function() {
    return Redirect::route('login');
});


Route::get('/login/', array(
    'as'    => 'login',
    'uses'  => 'Controller@login'
));

Controller.php:

public function login()
{
    return 'Login page';
}

When I go to htdocs/laravel_project/, I get redirected to htdocs/laravel_project/login but I want it to be htdocs/laravel_project/login/ I want to add that slash to the end of the URL. If I do manually enter the slash at the URL it does what I want.


回答1:


You are calling Redirect::route which translates to:

Redirect to the URL (with trailing slash trimmed) of a corresponding route.

Notice that Laravel will automatically remove the trailing slash of generated URL.

So, without further/deeper investigation, the fastest method would be:

return Redirect::to(URL::route('login') . '/');



回答2:


This worked for me:

Redirect::to('example/page' . '\/', 301);

Use Redirect::to instead of Redirect::route and add . '\/' to the route



来源:https://stackoverflow.com/questions/24728417/laravel-add-trailing-slash-with-redirectroute

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