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);
}
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.
The redirect() function actually don't return a RedirectResponse, it is returning a Illuminate\Routing\Redirector. This class do have the route() method.
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community