什么时候使用:redirect('/')vs. redirect()-> route('home')vs. redirect()-> home()? [英] When to use: redirect('/') vs. redirect()->route('home') vs. redirect()->home()?

查看:69
本文介绍了什么时候使用:redirect('/')vs. redirect()-> route('home')vs. redirect()-> home()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我有这个命名的路线时:
Route::get('/', 'IndexController@index')->name('home');

然后使用任何Controller的任何操作方法;当我需要重定向到命名路由 home 时;这些语句中的任何一条都可以正确地重定向到预期的路线:

return redirect('/');
return redirect()->route('home');
return redirect()->home();

何时使用它们?
有什么区别?
相对于其他使用,有什么好处吗?

解决方案

作为文档提及:

当您不带任何参数调用重定向助手时, Illuminate \ Routing \ Redirector 返回,允许您调用任何 Redirector实例上的方法.例如,要生成一个 RedirectResponse到已命名的路由,您可以使用route方法

正如您在API方法中看到的那样(下面的链接),您可以使用很多方法,并且还有一个特定的辅助方法home(),它只是redirect()-> route('home '),由@ceejayoz突出显示.

现在,我们将讨论return redirect('/');return redirect()->route('home');,正如您说的 BUT 一样,它们中的两个正确地重定向到了预定的路由,如果将来,第二个真的很有用.

为什么?

因为如果您要更改路由文件中的URL结构,则只需更改路由即可,例如:

Route::get('/', 'IndexController@index')->name('home');

将会:

Route::get('/home_page', 'IndexController@index')->name('home');

并且所有重定向都将引用该路由,并且您无需更改其他任何内容=>所有重定向仍将正常运行.

但是

如果您选择使用第一个(我的意思是return redirect('/');),那么在更改路由之后,您将需要解析所有控制器,以检查是否存在某些使用重定向的路由,然后更改了路由并对其进行了更改: p

When I have this named route:
Route::get('/', 'IndexController@index')->name('home');

Then in any action method of any Controller; when I need to redirect to the named route home; any of these statements redirects properly to the intended route:

return redirect('/');
return redirect()->route('home');
return redirect()->home();

When to use each?
What are the differences?
Are there any benefits of using one over the others?

解决方案

As the documentation mention :

When you call the redirect helper with no parameters, an instance of Illuminate\Routing\Redirector is returned, allowing you to call any method on the Redirector instance. For example, to generate a RedirectResponse to a named route, you may use the route method

As you can see in the API methods(link below) there is a lot of methods that you can use and also there is one specific helper method home() it's just a shortcut for redirect()->route('home') as highlighted by @ceejayoz.

Now the we will talk about return redirect('/'); and return redirect()->route('home'); the two of them redirects properly to the intended route as you said BUT the second one is really useful if in the future.

Why ?

Because if you want to change the URL structure in the routes file all you would need to change is the route only for example :

Route::get('/', 'IndexController@index')->name('home');

Will be :

Route::get('/home_page', 'IndexController@index')->name('home');

and all the redirects would refer to that route and there is no other thing that you should change => all redirects will still work perfectly.

BUT

If you choose to use the first one (i mean return redirect('/');) then after the change in the route you will need to parse all your controllers to check if there is some redirects that uses then changed route and the change them :p

这篇关于什么时候使用:redirect('/')vs. redirect()-> route('home')vs. redirect()-> home()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆