This article was originally published on bmf-tech.com.
It's a trivial matter, but I was curious, so I looked into it.
public function getIndex()
{
return redirect()->to('hoge');
}
I've been using this one for some reason,
public function getIndex()
{
return redirect('hoge');
}
But both work without any issues, so I investigated the implementation of the redirect helper.
Implementation of the redirect Helper
if (!function_exists('redirect')) {
/**
* Get an instance of the redirector.
*
* @param string|null $to
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
*/
function redirect($to = null, $status = 302, $headers = [], $secure = null)
{
if (is_null($to)) {
return app('redirect');
}
return app('redirect')->to($to, $status, $headers, $secure);
}
}
It seems that when the argument is empty, it calls the instance. It was also mentioned in the documentation lol.
The API of the called instance can be found here.
The implementation of the to method is as follows:
/**
* Create a new redirect response to the given path.
*
* @param string $path
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Illuminate\Http\RedirectResponse
*/
public function to($path, $status = 302, $headers = [], $secure = null)
{
$path = $this->generator->to($path, [], $secure);
return $this->createRedirect($path, $status, $headers);
}
Conclusion
redirect('hoge') and redirect()->to('hoge') are the same.
If you just want a simple redirect, use redirect('hoge'). If you want to carry flash data or redirect to a controller method, use redirect() which returns an instance with an empty argument.
Note
I thought I should check the implementation of the code I've been unconsciously writing since I started using Laravel φ(..)
Top comments (0)