phplaravelhttp-redirectroutesurl-routing

Laravel - redirect() vs redirect()->route() vs to_route()


In Laravel 10, let's suppose I have this route in my web.php file:

Route::get('/whatever', function() {
    return response()->json([
        'message' => 'hello from whatever route'
    ]);
})->name('whatever');

Now, I want to redirect from the route / to the route /whatever. I can use three statements that achieve the same results.

Route::get('/', function () {
    return redirect('whatever'); // works
    return redirect()->route('whatever'); // works
    return to_route('whatever') // works
});

So, what is the difference between them?


Solution

  • Your question is complicated by the fact that the URL address matches the Route Name. Therefore, I will illustrate the difference between the three functions with a new example.

    Example

    Route::get('/path/to/whatever', function() {
        // ...
    })->name('example-whatever-name');
    

    What is a redirect() helper

    The redirect() is a "helper" in Laravel that performs a redirection. There are multiple ways to redirect a user to a new URL. If you provide the first parameter to the redirect function, you don't have to specify the Route Name, instead, you provide the exact URL address.

    Upon invocation, it passes the parameter to the to() function of the Redirect class, which expects a URL address. Therefore, the original function was redirect()->to(), and its abbreviation became the redirect() helper when accepting a parameter.

    return redirect('/path/to/whatever'); // an alternative to "redirect()->to()"
    
    return redirect()->to('/path/to/whatever'); // an alternative to "Redirect::to()"
    
    use Illuminate\Support\Facades\Redirect;
    
    return Redirect::to('/path/to/whatever');
    

    It is evident that among the three pieces of code, the first one is the most convenient.

    For a better understanding, it is recommended to review the source code of the framework as well:

    It is clearly visible what happens when there is a parameter and when there is no parameter.

    Calling redirect() without parameters, interpretation of the Redirect class

    If no parameter is provided to the redirect() function, it returns an instance of the Redirect class, where various functions can be called. Examples of such functions include "action", "route", etc. You are interested in the route() function.

    For this route() function, instead of providing a URL address, you need to pass the Route Name as a parameter, and it redirects to the appropriate route based on the route name.

    return redirect()->route('example-whatever-name'); // an alternative to "Redirect::route()"
    
    use Illuminate\Support\Facades\Redirect;
    
    return Redirect::route('example-whatever-name');
    

    It is evident that among the two pieces of code, the first one is the most convenient, but (under the following title) using to_route() will be an even better choice.

    What is a to_route() helper function

    Since Laravel 9.x, the to_route() global function has been introduced to the framework. This is nothing more than a shorter alternative to redirect()->route(). In most cases, redirecting with the Route Name is much better and more readable than using a URL address. Therefore, it is wasteful to type out redirect()->route() for every redirection. With its abbreviated version, your code becomes shorter and perhaps more readable.

    return to_route('example-whatever-name'); // an alternative to "redirect()->route()"
    

    For a better understanding, it is recommended to review the source code of the framework as well:

    Extra: more information about Redirect::class

    The Redirect class is what allows us to perform redirections. The previously mentioned redirect() "helper" simply declares this class when called without parameters. Or, when called with a parameter, it immediately passes the parameter to the Redirect::to() function.

    To avoid the use of many use statements, "helper" functions were introduced, such as the mentioned redirect(), which circumvents the need for importing and using \Illuminate\Support\Facades\Redirect.