blob: a3d67b84728d6cee8c6f6c738d318e728de25338 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001############
2Output Class
3############
4
5The Output class is a small class with one main function: To send the
6finalized web page to the requesting browser. It is also responsible for
7:doc:`caching <../general/caching>` your web pages, if you use that
8feature.
9
10.. note:: This class is initialized automatically by the system so there
11 is no need to do it manually.
12
13Under normal circumstances you won't even notice the Output class since
14it works transparently without your intervention. For example, when you
15use the :doc:`Loader <../libraries/loader>` class to load a view file,
16it's automatically passed to the Output class, which will be called
17automatically by CodeIgniter at the end of system execution. It is
18possible, however, for you to manually intervene with the output if you
19need to, using either of the two following functions:
20
21$this->output->set_output();
22=============================
23
24Permits you to manually set the final output string. Usage example::
25
26 $this->output->set_output($data);
27
28.. important:: If you do set your output manually, it must be the last
29 thing done in the function you call it from. For example, if you build a
30 page in one of your controller functions, don't set the output until the
31 end.
32
33$this->output->set_content_type();
34====================================
35
36Permits you to set the mime-type of your page so you can serve JSON
37data, JPEG's, XML, etc easily.
38
39::
40
41 $this->output
42 ->set_content_type('application/json')
43 ->set_output(json_encode(array('foo' => 'bar')));
44
45 $this->output
46 ->set_content_type('jpeg') // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php
47 ->set_output(file_get_contents('files/something.jpg'));
48
49.. important:: Make sure any non-mime string you pass to this method
50 exists in config/mimes.php or it will have no effect.
51
Andrey Andreev47b67332012-06-06 15:58:05 +030052You can also set the character set of the document, by passing a second argument::
53
54 $this->output->set_content_type('css', 'utf-8');
55
Andrey Andreevcc4b0032012-11-29 17:21:43 +020056$this->output->get_content_type()
57=================================
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070058
Andrey Andreevcc4b0032012-11-29 17:21:43 +020059Returns the Content-Type HTTP header that's currently in use,
60excluding the character set value.
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070061
Andrey Andreev00adf1d2012-04-03 12:30:50 +030062 $mime = $this->output->get_content_type();
63
64.. note:: If not set, the default return value is 'text/html'.
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070065
Andrey Andreevcc4b0032012-11-29 17:21:43 +020066$this->output->get_header()
67===========================
68
69Gets the requested HTTP header value, if set.
70
71If the header is not set, NULL will be returned.
72If an empty value is passed to the method, it will return FALSE.
73
74Example::
75
76 $this->output->set_content_type('text/plain', 'UTF-8');
77 echo $this->output->get_header('content-type');
78 // Outputs: text/plain; charset=utf-8
79
80.. note:: The header name is compared in a case-insensitive manner.
81
82.. note:: Raw headers sent via PHP's native ``header()`` function are
83 also detected.
84
85$this->output->get_output()
86===========================
Derek Jones8ede1a22011-10-05 13:34:52 -050087
88Permits you to manually retrieve any output that has been sent for
89storage in the output class. Usage example::
90
91 $string = $this->output->get_output();
92
93Note that data will only be retrievable from this function if it has
94been previously sent to the output class by one of the CodeIgniter
95functions like $this->load->view().
96
97$this->output->append_output();
98================================
99
100Appends data onto the output string. Usage example::
101
102 $this->output->append_output($data);
103
104$this->output->set_header();
105=============================
106
107Permits you to manually set server headers, which the output class will
108send for you when outputting the final rendered display. Example::
109
Derek Jones36be9692011-10-05 15:52:41 -0500110 $this->output->set_header("HTTP/1.0 200 OK");
111 $this->output->set_header("HTTP/1.1 200 OK");
112 $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
113 $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
114 $this->output->set_header("Cache-Control: post-check=0, pre-check=0");
115 $this->output->set_header("Pragma: no-cache");
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
117$this->output->set_status_header(code, 'text');
118=================================================
119
120Permits you to manually set a server status header. Example::
121
Derek Jones36be9692011-10-05 15:52:41 -0500122 $this->output->set_status_header('401');
123 // Sets the header as: Unauthorized
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
125`See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for
126a full list of headers.
127
Andrey Andreev3639d472012-10-24 11:43:21 +0300128.. note:: This method is an alias for :doc:`Common function <../general/common_functions>`
Andrey Andreev956631d2012-10-06 20:43:47 +0300129 ``set_status_header()``.
130
Derek Jones8ede1a22011-10-05 13:34:52 -0500131$this->output->enable_profiler();
132==================================
133
134Permits you to enable/disable the
135:doc:`Profiler <../general/profiling>`, which will display benchmark
136and other data at the bottom of your pages for debugging and
137optimization purposes.
138
139To enable the profiler place the following function anywhere within your
140:doc:`Controller <../general/controllers>` functions::
141
142 $this->output->enable_profiler(TRUE);
143
144When enabled a report will be generated and inserted at the bottom of
145your pages.
146
147To disable the profiler you will use::
148
149 $this->output->enable_profiler(FALSE);
150
151$this->output->set_profiler_sections();
152=========================================
153
154Permits you to enable/disable specific sections of the Profiler when
155enabled. Please refer to the :doc:`Profiler <../general/profiling>`
156documentation for further information.
157
158$this->output->cache();
159=======================
160
161The CodeIgniter output library also controls caching. For more
162information, please see the :doc:`caching
163documentation <../general/caching>`.
164
165Parsing Execution Variables
166===========================
167
168CodeIgniter will parse the pseudo-variables {elapsed_time} and
169{memory_usage} in your output by default. To disable this, set the
170$parse_exec_vars class property to FALSE in your controller.
171::
172
173 $this->output->parse_exec_vars = FALSE;
174