Skip to content

Commit 71449dd

Browse files
committed
docs: add "Redirect" section in HTTP Responses and move the description
1 parent bc90d54 commit 71449dd

2 files changed

Lines changed: 54 additions & 41 deletions

File tree

user_guide_src/source/general/common_functions.rst

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -320,48 +320,8 @@ Miscellaneous Functions
320320
:param string $route: The route name or Controller::method to redirect the user to.
321321
:rtype: RedirectResponse
322322

323-
.. important:: When you use this function, an instance of ``RedirectResponse`` must be returned
324-
in the method of the :doc:`Controller <../incoming/controllers>` or
325-
the :doc:`Controller Filter <../incoming/filters>`. If you forget to return it,
326-
no redirection will occur.
327-
328323
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()``:
333-
334-
.. literalinclude:: common_functions/005.php
335-
:lines: 2-
336-
337-
.. note:: If there is a fragment in your URL that you want to remove, you can use the refresh parameter in this function.
338-
Like ``return redirect()->to('to', null, 'refresh');``.
339-
340-
**Redirect to a Defined Route**
341-
342-
When you want to pass a :ref:`route name <using-named-routes>` or Controller::method
343-
for :ref:`reverse routing <reverse-routing>`, use ``redirect()->route()``:
344-
345-
.. literalinclude:: common_functions/013.php
346-
:lines: 2-
347-
348-
When passing an argument into the function, it is treated as a route name or
349-
Controller::method for reverse routing, not a relative/full URI,
350-
treating it the same as using ``redirect()->route()``:
351-
352-
.. literalinclude:: common_functions/006.php
353-
:lines: 2-
354-
355-
**Redirect Back**
356-
357-
When you want to redirect back, use ``redirect()->back()``:
358-
359-
.. literalinclude:: common_functions/014.php
360-
:lines: 2-
361-
362-
.. note:: ``redirect()->back()`` is not the same as browser "back" button.
363-
It takes a visitor to "the last page viewed during the Session" when the Session is available.
364-
If the Session hasn’t been loaded, or is otherwise unavailable, then a sanitized version of HTTP_REFERER will be used.
324+
See :ref:`response-redirect` for details.
365325

366326
.. php:function:: remove_invisible_characters($str[, $urlEncoded = true])
367327

user_guide_src/source/outgoing/response.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,59 @@ parameter. This is not case-sensitive.
5959

6060
.. literalinclude:: response/006.php
6161

62+
.. _response-redirect:
63+
64+
Redirect
65+
========
66+
67+
If you want to create a redirect, use the :php:func:`redirect()` function. It
68+
returns a ``RedirectResponse`` instance.
69+
70+
.. important:: If you want to redirect, an instance of ``RedirectResponse`` must
71+
be returned in a method of the :doc:`Controller <../incoming/controllers>` or
72+
the :doc:`Controller Filter <../incoming/filters>`. Note that the ``__construct()``
73+
or the ``initController()`` method cannot return any value.
74+
If you forget to return ``RedirectResponse``, no redirection will occur.
75+
76+
Redirect to a URI path
77+
----------------------
78+
79+
When you want to pass a URI path (relative to baseURL), use ``redirect()->to()``:
80+
81+
.. literalinclude:: ../general/common_functions/005.php
82+
:lines: 2-
83+
84+
.. note:: If there is a fragment in your URL that you want to remove, you can use the refresh parameter in this function.
85+
Like ``return redirect()->to('to', null, 'refresh');``.
86+
87+
Redirect to a Defined Route
88+
---------------------------
89+
90+
When you want to pass a :ref:`route name <using-named-routes>` or Controller::method
91+
for :ref:`reverse routing <reverse-routing>`, use ``redirect()->route()``:
92+
93+
.. literalinclude:: ../general/common_functions/013.php
94+
:lines: 2-
95+
96+
When passing an argument into the function, it is treated as a route name or
97+
Controller::method for reverse routing, not a relative/full URI,
98+
treating it the same as using ``redirect()->route()``:
99+
100+
.. literalinclude:: ../general/common_functions/006.php
101+
:lines: 2-
102+
103+
Redirect Back
104+
-------------
105+
106+
When you want to redirect back, use ``redirect()->back()``:
107+
108+
.. literalinclude:: ../general/common_functions/014.php
109+
:lines: 2-
110+
111+
.. note:: ``redirect()->back()`` is not the same as browser "back" button.
112+
It takes a visitor to "the last page viewed during the Session" when the Session is available.
113+
If the Session hasn’t been loaded, or is otherwise unavailable, then a sanitized version of HTTP_REFERER will be used.
114+
62115
.. _force-file-download:
63116

64117
Force File Download

0 commit comments

Comments
 (0)