@@ -12,8 +12,11 @@ a server responding to the client that called it.
12
12
Working with the Response
13
13
=========================
14
14
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
17
20
sending the headers and the body for you. This is great if the page successfully created the content it was asked to.
18
21
When things go wrong, or you need to send very specific status codes back, or even take advantage of the
19
22
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
40
43
Setting Headers
41
44
---------------
42
45
46
+ setHeader()
47
+ ^^^^^^^^^^^
48
+
43
49
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,
45
53
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
+
46
57
Using these functions instead of using the native PHP functions allows you to ensure that no headers are sent
47
58
prematurely, causing errors, and makes testing possible.
48
59
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
+ ^^^^^^^^^^^^^^
50
66
51
67
If the header exists and can have more than one value, you may use the ``appendHeader() `` and ``prependHeader() ``
52
68
methods to add the value to the end or beginning of the values list, respectively. The first parameter is the name
53
69
of the header, while the second is the value to append or prepend.
54
70
55
71
.. literalinclude :: response/005.php
56
72
73
+ removeHeader()
74
+ ^^^^^^^^^^^^^^
75
+
57
76
Headers can be removed from the response with the ``removeHeader() `` method, which takes the header name as the only
58
77
parameter. This is not case-sensitive.
59
78
@@ -384,7 +403,9 @@ The methods provided by the parent class that are available are:
384
403
.. note :: Prior to v4.2.7, the default values of ``$secure`` and ``$httponly`` were ``false``
385
404
due to a bug, and these values from **app/Config/Cookie.php ** were never used.
386
405
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
388
409
pass information to this method so that a cookie can be set: Array
389
410
Method, and Discrete Parameters:
390
411
@@ -439,6 +460,8 @@ The methods provided by the parent class that are available are:
439
460
440
461
Delete an existing cookie.
441
462
463
+ .. note :: This also just sets browser cookie for deleting the cookie.
464
+
442
465
Only the ``name `` is required.
443
466
444
467
The ``prefix `` is only needed if you need to avoid name collisions with
0 commit comments