简体   繁体   English

在Laravel 5.2重定向中使用哪个更好?

[英]Which is better to use in Laravel 5.2 redirection?

I am newbie in Laravel and I am using Version 5.2, and I wanna know what is better to use in return value on redirecting routes in my Controller. 我是Laravel的新手,并且正在使用5.2版,我想知道在控制器中重定向路由时最好在返回值中使用什么。

return Redirect::route('home');

or 要么

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

Please indicate your source(s) in there are any. 请指出您的来源。

Michel , thats the same; 米歇尔,那是一样的。 as you already know so use whatever you are comfortable with, 如您所知,请使用您喜欢的任何东西,

but i think there are no performance boost or security risks or whatever you might be afraid of. 但我认为没有性能提升或安全隐患,也没有您可能担心的任何问题。 Those are static functions. 这些是静态函数。

Some says that you should not use a facade but thats wrong. 有人说您不应该使用外墙,但这是错误的。 unless you realy mess things up 除非你真的搞砸了

You can find more informations about it here , writen by the creator of laravel 您可以在此处找到有关它的更多信息,该信息由laravel的创建者撰写

HERE 这里

Both redirect() helper and Redirect facade do the same thing — they return the Redirector instance. redirect() helper和Redirect Facade都做同样的事情-它们返回Redirector实例。 So it's just the matter of your convenience. 因此,这只是您方便的问题。

I personally prefer using the helper, as shown in the docs . 我个人更喜欢使用helper,如docs所示。

It is totally depends on your preference.You can call name route with below way also: 这完全取决于您的喜好。您也可以通过以下方式调用名称route

return redirect(route('home'));

or 要么

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

or 要么

return Redirect::route('home');

I personally prefer first one. 我个人更喜欢第一个。

It's about your preferences, because these are just shortcuts and both do exactly the same . 这与您的喜好有关,因为它们只是快捷方式, 两者的作用完全相同 Personally I prefer to use helpers instead of facades. 我个人更喜欢使用助手而不是外墙。

tl;dr Move away from Facades. tl; dr远离立面。

Ultimately both will give you the desired outcome because the Laravel IoC container will resolve them to the same underlying class. 最终,两者都会为您提供所需的结果,因为Laravel IoC容器会将它们解析为同一基础类。

Many people prefer Facades for visual appeal. 许多人更喜欢Facades以获得视觉吸引力。

If this does not apply to you, consider code explicitness and modularity . 如果这不适用于您,请考虑代码的显式性和模块化

Constructor injection means that a class' dependencies are injected via the constructor when that class is created. 构造函数注入意味着在创建类时通过构造函数注入类的依赖项。 It is an explicit declaration of what that class needs, and therefore gives an idea as to what that class does. 它是该类所需内容的显式声明,因此可以使您对该类的用途有所了解。

Source: Taylor Otwell (creator of Laravel) blog post Response: Don't Use Facades 来源:Taylor Otwell(Laravel的创建者)博客文章回复:请勿使用立面

with Laravel 5, we have illuminate/contracts and method injection. 使用Laravel 5,我们有启发/合同和方法注入。 This makes it significantly easier to achieve the same end result as a facade would, while also decoupling our code in the process. 这使得获得与facade相同的最终结果变得更加容易,同时也使过程中的代码脱钩。

Source: Jeffrey Way (creator of laracasts) forum response To use or not to use Facades? 来源:Jeffrey Way(laracasts的创建者)论坛回复使用或不使用Facades?

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

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