In addition to Pablo's answer, I am going to add the code which shows that both methods use the same code, both the redirection and the way to pass data to the session.
Regarding redirection, the first code shown in the question calls this method:
link
/**
* 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)
{
return $this->createRedirect($this->generator->to($path, [], $secure), $status, $headers);
}
Next the code that is called by the second part of the question. Here, basically, the function to()
is called, which is what the OP uses in the first code that is shown and that is just before this text.
link
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);
}
}
Regarding the way to temporarily pass data to the session, the first code uses this method:
link
/**
* Flash a key / value pair to the session.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function flash($key, $value)
{
$this->put($key, $value);
$this->push('_flash.new', $key);
$this->removeFromOldFlashData([$key]);
}
The second method, like in the redirection, calls the code shown in the first part:
link
/**
* Flash a piece of data to the session.
*
* @param string|array $key
* @param mixed $value
* @return \Illuminate\Http\RedirectResponse
*/
public function with($key, $value = null)
{
$key = is_array($key) ? $key : [$key => $value];
foreach ($key as $k => $v) {
$this->session->flash($k, $v);
}
return $this;
In summary, the difference between the two ways of redirecting is practically none, except that the second includes "one more step" when calling another function.
The difference between the two ways of passing data to the session, apart from what Pablo already pointed out, is that the first one only allows to pass "one data", while in the second one you can pass an array of data as you can see in the code .