🎓 A1Learner
🔍
0 of 50 lessons completed

Laravel Redirects

A redirect is a response that sends the browser to a different address. A route returns a redirect when the reader belongs on another page.

Redirect to an address

The go route sends the browser to the target route.

return redirect('/l22/target');

Redirect to a named route

A route name keeps the address in one place.

return redirect()->route('l22.target');

Redirect back

The back method returns the reader to the last page.

return redirect()->back();

Example

Route::get('/l22/go', function () {
    return redirect('/l22/target');
});

Route::get('/l22/target', function () {
    return 'You arrived';
});

Key points

  • Use redirect() with an address to send the browser there.
  • Use redirect()->route() with a route name to send the browser to a named route.
  • Use redirect()->back() to send the browser to the page it came from.
  • A redirect uses status 302.

In this example, the go route returns a redirect to /l22/target. The browser then loads the target route. The target route returns You arrived. A redirect is a response that sends the browser to a different address.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does redirect('/l22/target') do?