Skip to content

Commit d95d86e

Browse files
committed
docs: improve redirect() explanation
1 parent 46a5d1e commit d95d86e

5 files changed

Lines changed: 51 additions & 27 deletions

File tree

user_guide_src/source/general/common_functions.rst

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,26 +317,48 @@ Miscellaneous Functions
317317

318318
.. php:function:: redirect(string $route)
319319
320-
:param string $route: The reverse-routed or named route to redirect the user to.
320+
:param string $route: The route name or Controller::method to redirect the user to.
321321
:rtype: RedirectResponse
322322

323323
.. important:: When you use this function, an instance of ``RedirectResponse`` must be returned
324324
in the method of the :doc:`Controller <../incoming/controllers>` or
325325
the :doc:`Controller Filter <../incoming/filters>`. If you forget to return it,
326326
no redirection will occur.
327327

328-
Returns a RedirectResponse instance allowing you to easily create redirects:
328+
Returns a RedirectResponse instance allowing you to easily create redirects.
329+
330+
**Redirect to a URI path**
331+
332+
When you want to pass a URI path (relative to baseURL), use ``redirect()->to()``:
329333

330334
.. literalinclude:: common_functions/005.php
335+
:lines: 2-
331336

332-
.. note:: ``redirect()->back()`` is not the same as browser "back" button.
333-
It takes a visitor to "the last page viewed during the Session" when the Session is available.
334-
If the Session hasn’t been loaded, or is otherwise unavailable, then a sanitized version of HTTP_REFERER will be used.
337+
**Redirect to a Defined Route**
338+
339+
When you want to pass a :ref:`route name <using-named-routes>` or Controller::method
340+
for :ref:`reverse routing <reverse-routing>`, use ``redirect()->route()``:
341+
342+
.. literalinclude:: common_functions/013.php
343+
:lines: 2-
335344

336-
When passing an argument into the function, it is treated as a named/reverse-routed route, not a relative/full URI,
345+
When passing an argument into the function, it is treated as a route name or
346+
Controller::method for reverse routing, not a relative/full URI,
337347
treating it the same as using ``redirect()->route()``:
338348

339349
.. literalinclude:: common_functions/006.php
350+
:lines: 2-
351+
352+
**Redirect Back**
353+
354+
When you want to redirect back, use ``redirect()->back()``:
355+
356+
.. literalinclude:: common_functions/014.php
357+
:lines: 2-
358+
359+
.. note:: ``redirect()->back()`` is not the same as browser "back" button.
360+
It takes a visitor to "the last page viewed during the Session" when the Session is available.
361+
If the Session hasn’t been loaded, or is otherwise unavailable, then a sanitized version of HTTP_REFERER will be used.
340362

341363
.. php:function:: remove_invisible_characters($str[, $urlEncoded = true])
342364
Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
11
<?php
22

3-
// Go back to the previous page
4-
return redirect()->back();
5-
6-
// Go to specific URI
7-
return redirect()->to('/admin');
8-
9-
// Go to a named route
10-
return redirect()->route('named_route');
11-
12-
// Keep the old input values upon redirect so they can be used by the `old()` function
13-
return redirect()->back()->withInput();
14-
15-
// Set a flash message
16-
return redirect()->back()->with('foo', 'message');
17-
18-
// Copies all cookies from global response instance
19-
return redirect()->back()->withCookies();
20-
21-
// Copies all headers from the global response instance
22-
return redirect()->back()->withHeaders();
3+
// Go to specific URI path. "admin/home" is the URI path relative to baseURL.
4+
return redirect()->to('admin/home');
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?php
22

3-
// Go to a named/reverse-routed URI
3+
// Go to a named/reverse-routed URI.
44
return redirect('named_route');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// Go to a named route. "user_gallery" is the route name, not a URI path.
4+
return redirect()->route('user_gallery');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
// Go back to the previous page.
4+
return redirect()->back();
5+
6+
// Keep the old input values upon redirect so they can be used by the `old()` function.
7+
return redirect()->back()->withInput();
8+
9+
// Set a flash message.
10+
return redirect()->back()->with('foo', 'message');
11+
12+
// Copies all cookies from global response instance.
13+
return redirect()->back()->withCookies();
14+
15+
// Copies all headers from the global response instance.
16+
return redirect()->back()->withHeaders();

0 commit comments

Comments
 (0)