What is the difference between these addresses in laravel?

1

I am new to laravel and I would like to know what is the difference between these two ways of redirecting and sending a message in laravel. First way:

Session::flash('message','Usuario actualizado correctamente');
return redirect::to('/usuario');

Second form:

return redirect('/usuario')->with('message','Usuario actualizado correctamente');
    
asked by Zenaido Hernandez Gonzalez 02.05.2017 в 07:43
source

2 answers

1

Both work in the same way.

In the First:

Session :: flash ('message', 'User updated successfully'); return redirect :: to ('/ user');

The documentation says:

  

Flash data

     

Sometimes you may want to store items in the session only for the next request. You can do it using the flash method. The data stored in the session using this method will only be available during the subsequent HTTP request and will then be deleted ..

This means that it stores flash data for when redirects.

Unlike the second, you can make a condition with different flash and then redirect. Example:

$exists = false;
if($exists){
   Session::flash('message','Usuario actualizado correctamente');
}else{
   Session::flash('message','Error al actualizar Usuario');
}
return redirect::to('/usuario');

In the Second:

return redirect ('/ user') -> with ('message', 'User updated successfully');

The documentation says:

  

Redirecting with Flashed Session Data

     

Redirection to a new URL and Flashed Session Data is normally done at the same time.

This means that it sends the data you want to send to the session to the new URL, unlike the first one, you can not create conditions for different flash, unless you make different redirects. Example:

$exists = false;
if($exists){
   return redirect('/usuario')->with('message','Usuario actualizado correctamente');
}else{
   return redirect('/usuario')->with('message','Error al actualizar Usuario');
}
    
answered by 02.05.2017 / 09:23
source
2

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 .

    
answered by 02.05.2017 в 16:46