When to use: redirect('/') vs. redirect()-

2019-02-26 03:01发布

问题:

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?

回答1:

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



回答2:

redirect()->home() is simply a shortcut for redirect()->route('home'). The source code can be seen here.

Named routes are generally better than raw URLs for maintainability purposes. The home route isn't all that likely to change location, but it is possible that you might host a Laravel app in a subfolder, or move the home page from / to /app to make room for a marketing landing page at the root.



回答3:

When you are passing a string it will redirect a user to the domain plus the string you pass.

http://localhost:3000 + string

It will also add / if you forget it, now if you name your routes like you did then you can call it by the name.
An advantage of using named routes is in case you want to change the URI you can do it without worrying about changing a bunch of ahref in your view, redirects in your controllers, etc. home() is a method from Laravel's Redirector or redirect() so, I don't think you can just call a named route as a method.



回答4:

redirect('/')

It redirects you to the base URL.

redirect()->route('home')

Redirects to the route named home. See More about named routes here.

 redirect()->home();

Alternative way to redirect to named route.Redirects to 'home' route as well. It does the same thing as above but with slightly different syntax.

I preferred named routes over raw URLs, because if you decide to change the URL later on, you have to make changes into your routes file only.