Support the ongoing development of Laravel.io →
Configuration Architecture Installation

Hello everyone, can you explain me how redirect()->route('home') works while redirect() return an instance of RedirectResponse and there is no route() function in RedirectResponse class.

I found that redirect is a function in helpers.php

 function redirect($to = null, $status = 302, $headers = [], $secure = null)
    {
        if (is_null($to)) {
            return app('redirect');
        }

        return app('redirect')->to($to, $status, $headers, $secure);
    }
}

and there are two route functions, one in helpers.php

if (!function_exists('route')) {
    /**
     * Generate a URL to a named route.
     *
     * @param  string  $name
     * @param  array   $parameters
     * @param  bool    $absolute
     * @param  \Illuminate\Routing\Route  $route
     * @return string
     */
    function route($name, $parameters = [], $absolute = true, $route = null)
    {
        return app('url')->route($name, $parameters, $absolute, $route);
    }
}

and there is one in Redirector class

    /**
     * Create a new redirect response to a named route.
     *
     * @param  string  $route
     * @param  array   $parameters
     * @param  int     $status
     * @param  array   $headers
     * @return \Illuminate\Http\RedirectResponse
     */
    public function route($route, $parameters = [], $status = 302, $headers = [])
    {
        $path = $this->generator->route($route, $parameters);

        return $this->to($path, $status, $headers);
    }
Last updated 3 years ago.
0

The helper function returns the URL that belongs to the route with that name. So you can pass the result of that function into the redirect function, which expects a URL.

return redirect(route('home'));

As second parameter of the route() function you can fill route parameters if needed.

0

The redirect() function actually don't return a RedirectResponse, it is returning a Illuminate\Routing\Redirector. This class do have the route() method.

0

ftiersch said:

The helper function returns the URL that belongs to the route with that name. So you can pass the result of that function into the redirect function, which expects a URL.

return redirect(route('home'));

As second parameter of the route() function you can fill route parameters if needed.

Thanks for your reply.

christoffertyrefors said:

The redirect() function actually don't return a RedirectResponse, it is returning a Illuminate\Routing\Redirector. This class do have the route() method.

Ok, I understand, thanks so much.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

buixuanhai buixuanhai Joined 20 Jun 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.