Skip to content

Commit d389049

Browse files
committed
docs: add explanation about redirect and Cookies/Headers
1 parent 6c187dd commit d389049

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

user_guide_src/source/outgoing/response.rst

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,15 @@ parameter. This is not case-sensitive.
6464
Redirect
6565
========
6666

67-
If you want to create a redirect, use the :php:func:`redirect()` function. It
68-
returns a ``RedirectResponse`` instance.
67+
If you want to create a redirect, use the :php:func:`redirect()` function.
68+
69+
It returns a ``RedirectResponse`` instance. It is a different instance from the
70+
global response instance that ``Services::response()`` returns.
71+
72+
.. warning:: If you set Cookies or Response Headers before you call ``redirect()``,
73+
they are set to the global response instance, and they are not automatically
74+
copied to the ``RedirectResponse`` instance. To send them, you need to call
75+
the ``withCookies()`` or ``withHeaders()`` method manually.
6976

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

123+
Redirect with Cookies
124+
---------------------
125+
126+
If you set Cookies before you call ``redirect()``, they are set to the global
127+
response instance, and they are not automatically copied to the ``RedirectResponse``
128+
instance.
129+
130+
To send the Cookies, you need to call the ``withCookies()`` method manually.
131+
132+
.. literalinclude:: ./response/034.php
133+
:lines: 2-
134+
135+
Redirect with Headers
136+
---------------------
137+
138+
If you set Response Headers before you call ``redirect()``, they are set to the
139+
global response instance, and they are not automatically copied to the
140+
``RedirectResponse`` instance.
141+
142+
To send the Headers, you need to call the ``withHeaders()`` method manually.
143+
144+
.. literalinclude:: ./response/035.php
145+
:lines: 2-
146+
116147
.. _response-redirect-status-code:
117148

118149
Redirect Status Code

user_guide_src/source/outgoing/response/031.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,3 @@
88

99
// Set a flash message.
1010
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();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// Copies all cookies from global response instance.
4+
return redirect()->back()->withCookies();
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
3+
// Copies all headers from the global response instance.
4+
return redirect()->back()->withHeaders();

0 commit comments

Comments
 (0)