Skip to content

Commit

Permalink
Merge pull request #8165 from kenjis/docs-redirect-with-cookie-header
Browse files Browse the repository at this point in the history
docs: add explanation about redirect and Cookies/Headers
  • Loading branch information
kenjis authored Nov 7, 2023
2 parents 6014daf + d389049 commit 97e0292
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
35 changes: 33 additions & 2 deletions user_guide_src/source/outgoing/response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ parameter. This is not case-sensitive.
Redirect
========

If you want to create a redirect, use the :php:func:`redirect()` function. It
returns a ``RedirectResponse`` instance.
If you want to create a redirect, use the :php:func:`redirect()` function.

It returns a ``RedirectResponse`` instance. It is a different instance from the
global response instance that ``Services::response()`` returns.

.. warning:: If you set Cookies or Response Headers before you call ``redirect()``,
they are set to the global response instance, and they are not automatically
copied to the ``RedirectResponse`` instance. To send them, you need to call
the ``withCookies()`` or ``withHeaders()`` method manually.

.. important:: If you want to redirect, an instance of ``RedirectResponse`` must
be returned in a method of the :doc:`Controller <../incoming/controllers>` or
Expand Down Expand Up @@ -113,6 +120,30 @@ When you want to redirect back, use ``redirect()->back()``:
It takes a visitor to "the last page viewed during the Session" when the Session is available.
If the Session hasn't been loaded, or is otherwise unavailable, then a sanitized version of HTTP_REFERER will be used.

Redirect with Cookies
---------------------

If you set Cookies before you call ``redirect()``, they are set to the global
response instance, and they are not automatically copied to the ``RedirectResponse``
instance.

To send the Cookies, you need to call the ``withCookies()`` method manually.

.. literalinclude:: ./response/034.php
:lines: 2-

Redirect with Headers
---------------------

If you set Response Headers before you call ``redirect()``, they are set to the
global response instance, and they are not automatically copied to the
``RedirectResponse`` instance.

To send the Headers, you need to call the ``withHeaders()`` method manually.

.. literalinclude:: ./response/035.php
:lines: 2-

.. _response-redirect-status-code:

Redirect Status Code
Expand Down
6 changes: 0 additions & 6 deletions user_guide_src/source/outgoing/response/031.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,3 @@

// Set a flash message.
return redirect()->back()->with('foo', 'message');

// Copies all cookies from global response instance.
return redirect()->back()->withCookies();

// Copies all headers from the global response instance.
return redirect()->back()->withHeaders();
4 changes: 4 additions & 0 deletions user_guide_src/source/outgoing/response/034.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// Copies all cookies from global response instance.
return redirect()->back()->withCookies();
4 changes: 4 additions & 0 deletions user_guide_src/source/outgoing/response/035.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

// Copies all headers from the global response instance.
return redirect()->back()->withHeaders();

0 comments on commit 97e0292

Please sign in to comment.