Skip to content

Commit 22bf430

Browse files
authored
Merge pull request #8179 from kenjis/docs-about-cookie-setting
docs: add explanation for setting cookies/headers
2 parents 0ab4d67 + b0ac56d commit 22bf430

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

user_guide_src/source/helpers/cookie_helper.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ The following functions are available:
4242
a description of its use, as this function is an alias for
4343
:php:meth:`CodeIgniter\\HTTP\\Response::setCookie()`.
4444

45+
.. note:: This helper function just sets browser cookies to the global response
46+
instance that ``Services::response()`` returns. So, if you create and
47+
return another response instance (e.g., if you call :php:func:`redirect()`),
48+
the cookies set here will not be sent automatically.
49+
4550
.. php:function:: get_cookie($index[, $xssClean = false[, $prefix = '']])
4651
4752
:param string $index: Cookie name
@@ -78,6 +83,9 @@ The following functions are available:
7883
This function is otherwise identical to :php:func:`set_cookie()`, except that it
7984
does not have the ``value`` and ``expire`` parameters.
8085

86+
This also just sets browser cookies for deleting the cookies to the global
87+
response instance that ``Services::response()`` returns.
88+
8189
.. note:: When you use :php:func:`set_cookie()`,
8290
if the ``value`` is set to empty string and the ``expire`` is set to ``0``, the cookie will be deleted.
8391
If the ``value`` is set to non-empty string and the ``expire`` is set to ``0``, the cookie will only last as long as the browser is open.
@@ -95,4 +103,6 @@ The following functions are available:
95103
:param string $prefix: Cookie prefix
96104
:rtype: bool
97105

98-
Checks if a cookie exists by name. This is an alias of ``Response::hasCookie()``.
106+
Checks if a cookie exists by name in the global response instance that
107+
``Services::response()`` returns. This is an alias of
108+
:php:meth:`CodeIgniter\\HTTP\\Response::hasCookie()`.

user_guide_src/source/installation/upgrade_4xx.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ Helpers
142142
- In CI4, ``redirect()`` is completely changed from CI3's.
143143
- `redirect() Documentation CodeIgniter 3.X <https://codeigniter.com/userguide3/helpers/url_helper.html#redirect>`_
144144
- `redirect() Documentation CodeIgniter 4.X <../general/common_functions.html#redirect>`_
145-
- In CI4, ``redirect()`` returns a ``RedirectResponse`` instance instead of redirecting and terminating script execution. You must return it.
145+
- In CI4, :php:func:`redirect()` returns a ``RedirectResponse`` instance instead of
146+
redirecting and terminating script execution. You must return it from Controllers
147+
or Controller Filters.
148+
- Cookies and Headers you set before calling ``redirect()`` are not automatically
149+
carried over to a ``RedirectResponse``. You need to call ``withCookies()``
150+
or ``withHeaders()`` manually if you want to send them.
146151
- You need to change CI3's ``redirect('login/form')`` to ``return redirect()->to('login/form')``.
147152

148153
Hooks

user_guide_src/source/outgoing/response.rst

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ a server responding to the client that called it.
1212
Working with the Response
1313
=========================
1414

15-
A Response class is instantiated for you and passed into your controllers. It can be accessed through
16-
``$this->response``. Many times you will not need to touch the class directly, since CodeIgniter takes care of
15+
A Response class is instantiated for you and passed into your controllers. It can
16+
be accessed through ``$this->response``. It is the same instance that
17+
``Services::response()`` returns. We call it the global response instance.
18+
19+
Many times you will not need to touch the class directly, since CodeIgniter takes care of
1720
sending the headers and the body for you. This is great if the page successfully created the content it was asked to.
1821
When things go wrong, or you need to send very specific status codes back, or even take advantage of the
1922
powerful HTTP caching, it's there for you.
@@ -40,20 +43,36 @@ You can set format an array into either JSON or XML and set the content type hea
4043
Setting Headers
4144
---------------
4245

46+
setHeader()
47+
^^^^^^^^^^^
48+
4349
Often, you will need to set headers to be set for the response. The Response class makes this very simple to do,
44-
with the ``setHeader()`` method. The first parameter is the name of the header. The second parameter is the value,
50+
with the ``setHeader()`` method.
51+
52+
The first parameter is the name of the header. The second parameter is the value,
4553
which can be either a string or an array of values that will be combined correctly when sent to the client.
54+
55+
.. literalinclude:: response/004.php
56+
4657
Using these functions instead of using the native PHP functions allows you to ensure that no headers are sent
4758
prematurely, causing errors, and makes testing possible.
4859

49-
.. literalinclude:: response/004.php
60+
.. note:: This method just sets headers to the response instance. So, if you create
61+
and return another response instance (e.g., if you call :php:func:`redirect()`),
62+
the headers set here will not be sent automatically.
63+
64+
appendHeader()
65+
^^^^^^^^^^^^^^
5066

5167
If the header exists and can have more than one value, you may use the ``appendHeader()`` and ``prependHeader()``
5268
methods to add the value to the end or beginning of the values list, respectively. The first parameter is the name
5369
of the header, while the second is the value to append or prepend.
5470

5571
.. literalinclude:: response/005.php
5672

73+
removeHeader()
74+
^^^^^^^^^^^^^^
75+
5776
Headers can be removed from the response with the ``removeHeader()`` method, which takes the header name as the only
5877
parameter. This is not case-sensitive.
5978

@@ -384,7 +403,9 @@ The methods provided by the parent class that are available are:
384403
.. note:: Prior to v4.2.7, the default values of ``$secure`` and ``$httponly`` were ``false``
385404
due to a bug, and these values from **app/Config/Cookie.php** were never used.
386405

387-
Sets a cookie containing the values you specify. There are two ways to
406+
Sets a cookie containing the values you specify to the Response instance.
407+
408+
There are two ways to
388409
pass information to this method so that a cookie can be set: Array
389410
Method, and Discrete Parameters:
390411

@@ -439,6 +460,8 @@ The methods provided by the parent class that are available are:
439460

440461
Delete an existing cookie.
441462

463+
.. note:: This also just sets browser cookie for deleting the cookie.
464+
442465
Only the ``name`` is required.
443466

444467
The ``prefix`` is only needed if you need to avoid name collisions with

0 commit comments

Comments
 (0)