blob: 92060f66a8eb2f28b2ad586eb5f97165a2817b1c [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001############
2Output Class
3############
4
Andrey Andreevc8f34852014-01-03 13:47:27 +02005The Output class is a core class with one main function: To send the
Derek Jones8ede1a22011-10-05 13:34:52 -05006finalized 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
Andrey Andreevc8f34852014-01-03 13:47:27 +020019need to.
Derek Jones8ede1a22011-10-05 13:34:52 -050020
Andrey Andreevcc042092014-01-03 17:08:27 +020021.. contents::
22 :local:
23
24.. raw:: html
25
26 <div class="custom-index container"></div>
27
Andrey Andreevc8f34852014-01-03 13:47:27 +020028***************
29Class Reference
30***************
Derek Jones8ede1a22011-10-05 13:34:52 -050031
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020032.. php:class:: CI_Output
Derek Jones8ede1a22011-10-05 13:34:52 -050033
Andrey Andreevc8f34852014-01-03 13:47:27 +020034 .. attribute:: $parse_exec_vars = TRUE;
Derek Jones8ede1a22011-10-05 13:34:52 -050035
Andrey Andreevc8f34852014-01-03 13:47:27 +020036 Enables/disables parsing of the {elapsed_time} and {memory_usage} pseudo-variables.
Derek Jones8ede1a22011-10-05 13:34:52 -050037
Andrey Andreevc8f34852014-01-03 13:47:27 +020038 CodeIgniter will parse those tokens in your output by default. To disable this, set
39 this property to FALSE in your controller.
40 ::
Derek Jones8ede1a22011-10-05 13:34:52 -050041
Andrey Andreevc8f34852014-01-03 13:47:27 +020042 $this->output->parse_exec_vars = FALSE;
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020044 .. php:method:: set_output($output)
Derek Jones8ede1a22011-10-05 13:34:52 -050045
Andrey Andreev28c2c972014-02-08 04:27:48 +020046 :param string $output: String to set the output to
47 :returns: CI_Output instance (method chaining)
48 :rtype: CI_Output
Derek Jones8ede1a22011-10-05 13:34:52 -050049
Andrey Andreevc8f34852014-01-03 13:47:27 +020050 Permits you to manually set the final output string. Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -050051
Andrey Andreevc8f34852014-01-03 13:47:27 +020052 $this->output->set_output($data);
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Andrey Andreevc8f34852014-01-03 13:47:27 +020054 .. important:: If you do set your output manually, it must be the last thing done
55 in the function you call it from. For example, if you build a page in one
Andrey Andreev28c2c972014-02-08 04:27:48 +020056 of your controller methods, don't set the output until the end.
Andrey Andreev47b67332012-06-06 15:58:05 +030057
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020058 .. php:method:: set_content_type($mime_type[, $charset = NULL])
Andrey Andreev47b67332012-06-06 15:58:05 +030059
Andrey Andreev28c2c972014-02-08 04:27:48 +020060 :param string $mime_type: MIME Type idenitifer string
61 :param string $charset: Character set
62 :returns: CI_Output instance (method chaining)
63 :rtype: CI_Output
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070064
Andrey Andreevc8f34852014-01-03 13:47:27 +020065 Permits you to set the mime-type of your page so you can serve JSON data, JPEG's, XML, etc easily.
66 ::
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070067
Andrey Andreevc8f34852014-01-03 13:47:27 +020068 $this->output
69 ->set_content_type('application/json')
70 ->set_output(json_encode(array('foo' => 'bar')));
Andrey Andreev00adf1d2012-04-03 12:30:50 +030071
Andrey Andreevc8f34852014-01-03 13:47:27 +020072 $this->output
73 ->set_content_type('jpeg') // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php
74 ->set_output(file_get_contents('files/something.jpg'));
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070075
Andrey Andreevc8f34852014-01-03 13:47:27 +020076 .. important:: Make sure any non-mime string you pass to this method
77 exists in *application/config/mimes.php* or it will have no effect.
Andrey Andreevcc4b0032012-11-29 17:21:43 +020078
Andrey Andreevc8f34852014-01-03 13:47:27 +020079 You can also set the character set of the document, by passing a second argument::
Andrey Andreevcc4b0032012-11-29 17:21:43 +020080
Andrey Andreevc8f34852014-01-03 13:47:27 +020081 $this->output->set_content_type('css', 'utf-8');
Andrey Andreevcc4b0032012-11-29 17:21:43 +020082
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020083 .. php:method:: get_content_type()
Andrey Andreevcc4b0032012-11-29 17:21:43 +020084
Andrey Andreev28c2c972014-02-08 04:27:48 +020085 :returns: Content-Type string
86 :rtype: string
Andrey Andreevcc4b0032012-11-29 17:21:43 +020087
Andrey Andreevc8f34852014-01-03 13:47:27 +020088 Returns the Content-Type HTTP header that's currently in use, excluding the character set value.
89 ::
Andrey Andreevcc4b0032012-11-29 17:21:43 +020090
Andrey Andreevc8f34852014-01-03 13:47:27 +020091 $mime = $this->output->get_content_type();
Andrey Andreevcc4b0032012-11-29 17:21:43 +020092
Andrey Andreevc8f34852014-01-03 13:47:27 +020093 .. note:: If not set, the default return value is 'text/html'.
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020095 .. php:method:: get_header($header)
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Andrey Andreev28c2c972014-02-08 04:27:48 +020097 :param string $header: HTTP header name
98 :returns: HTTP response header or NULL if not found
99 :rtype: mixed
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
Andrey Andreevc8f34852014-01-03 13:47:27 +0200101 Returns the requested HTTP header value, or NULL if the requested header is not set.
102 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Andrey Andreevc8f34852014-01-03 13:47:27 +0200104 $this->output->set_content_type('text/plain', 'UTF-8');
105 echo $this->output->get_header('content-type');
106 // Outputs: text/plain; charset=utf-8
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Andrey Andreevc8f34852014-01-03 13:47:27 +0200108 .. note:: The header name is compared in a case-insensitive manner.
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
Andrey Andreevc8f34852014-01-03 13:47:27 +0200110 .. note:: Raw headers sent via PHP's native ``header()`` function are also detected.
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200112 .. php:method:: get_output()
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
Andrey Andreev28c2c972014-02-08 04:27:48 +0200114 :returns: Output string
115 :rtype: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Andrey Andreevc8f34852014-01-03 13:47:27 +0200117 Permits you to manually retrieve any output that has been sent for
118 storage in the output class. Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Andrey Andreevc8f34852014-01-03 13:47:27 +0200120 $string = $this->output->get_output();
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
Andrey Andreevc8f34852014-01-03 13:47:27 +0200122 Note that data will only be retrievable from this function if it has
123 been previously sent to the output class by one of the CodeIgniter
124 functions like ``$this->load->view()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200126 .. php:method:: append_output($output)
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Andrey Andreev28c2c972014-02-08 04:27:48 +0200128 :param string $output: Additional output data to append
129 :returns: CI_Output instance (method chaining)
130 :rtype: CI_Output
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
Andrey Andreevc8f34852014-01-03 13:47:27 +0200132 Appends data onto the output string.
133 ::
Andrey Andreev956631d2012-10-06 20:43:47 +0300134
Andrey Andreevc8f34852014-01-03 13:47:27 +0200135 $this->output->append_output($data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200137 .. php:method:: set_header($header[, $replace = TRUE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500138
Andrey Andreev28c2c972014-02-08 04:27:48 +0200139 :param string $header: HTTP response header
140 :param bool $replace: Whether to replace the old header value, if it is already set
141 :returns: CI_Output instance (method chaining)
142 :rtype: CI_Output
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Andrey Andreevc8f34852014-01-03 13:47:27 +0200144 Permits you to manually set server headers, which the output class will
145 send for you when outputting the final rendered display. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Andrey Andreevc8f34852014-01-03 13:47:27 +0200147 $this->output->set_header('HTTP/1.0 200 OK');
148 $this->output->set_header('HTTP/1.1 200 OK');
149 $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
150 $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
151 $this->output->set_header('Cache-Control: post-check=0, pre-check=0');
152 $this->output->set_header('Pragma: no-cache');
Derek Jones8ede1a22011-10-05 13:34:52 -0500153
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200154 .. php:method:: set_status_header([$code = 200[, $text = '']])
Derek Jones8ede1a22011-10-05 13:34:52 -0500155
Andrey Andreev28c2c972014-02-08 04:27:48 +0200156 :param int $code: HTTP status code
157 :param string $text: Optional message
158 :returns: CI_Output instance (method chaining)
159 :rtype: CI_Output
Derek Jones8ede1a22011-10-05 13:34:52 -0500160
Andrey Andreevc8f34852014-01-03 13:47:27 +0200161 Permits you to manually set a server status header. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
kenjis87ed4022015-07-17 11:29:51 +0900163 $this->output->set_status_header(401);
Andrey Andreevc8f34852014-01-03 13:47:27 +0200164 // Sets the header as: Unauthorized
Derek Jones8ede1a22011-10-05 13:34:52 -0500165
Andrey Andreevc8f34852014-01-03 13:47:27 +0200166 `See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for a full list of headers.
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Andrey Andreevc8f34852014-01-03 13:47:27 +0200168 .. note:: This method is an alias for :doc:`Common function <../general/common_functions>`
169 :func:`set_status_header()`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200171 .. php:method:: enable_profiler([$val = TRUE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
Andrey Andreev28c2c972014-02-08 04:27:48 +0200173 :param bool $val: Whether to enable or disable the Profiler
174 :returns: CI_Output instance (method chaining)
175 :rtype: CI_Output
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
Andrey Andreevc8f34852014-01-03 13:47:27 +0200177 Permits you to enable/disable the :doc:`Profiler <../general/profiling>`, which will display benchmark
178 and other data at the bottom of your pages for debugging and optimization purposes.
Derek Jones8ede1a22011-10-05 13:34:52 -0500179
Andrey Andreevc8f34852014-01-03 13:47:27 +0200180 To enable the profiler place the following line anywhere within your
181 :doc:`Controller <../general/controllers>` methods::
182
183 $this->output->enable_profiler(TRUE);
184
185 When enabled a report will be generated and inserted at the bottom of your pages.
186
187 To disable the profiler you would use::
188
189 $this->output->enable_profiler(FALSE);
190
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200191 .. php:method:: set_profiler_sections($sections)
Andrey Andreevc8f34852014-01-03 13:47:27 +0200192
Andrey Andreev28c2c972014-02-08 04:27:48 +0200193 :param array $sections: Profiler sections
194 :returns: CI_Output instance (method chaining)
195 :rtype: CI_Output
Andrey Andreevc8f34852014-01-03 13:47:27 +0200196
197 Permits you to enable/disable specific sections of the Profiler when it is enabled.
198 Please refer to the :doc:`Profiler <../general/profiling>` documentation for further information.
199
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200200 .. php:method:: cache($time)
Andrey Andreevc8f34852014-01-03 13:47:27 +0200201
Andrey Andreev22df06b2016-01-20 12:10:08 +0200202 :param int $time: Cache expiration time in minutes
Andrey Andreev28c2c972014-02-08 04:27:48 +0200203 :returns: CI_Output instance (method chaining)
204 :rtype: CI_Output
Andrey Andreevc8f34852014-01-03 13:47:27 +0200205
Andrey Andreev22df06b2016-01-20 12:10:08 +0200206 Caches the current page for the specified amount of minutes.
Andrey Andreevc8f34852014-01-03 13:47:27 +0200207
Ahmad Anbar93c09872014-05-31 18:52:13 +0300208 For more information, please see the :doc:`caching documentation <../general/caching>`.
209
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200210 .. php:method:: _display([$output = ''])
Ahmad Anbar93c09872014-05-31 18:52:13 +0300211
212 :param string $output: Output data override
213 :returns: void
214 :rtype: void
215
Ahmad Anbar054bbe82014-06-01 12:20:08 +0300216 Sends finalized output data to the browser along with any server headers. It also stops benchmark
217 timers.
Ahmad Anbar93c09872014-05-31 18:52:13 +0300218
Ahmad Anbare06c5c42014-06-01 12:22:12 +0300219 .. note:: This method is called automatically at the end of script execution, you won't need to
220 call it manually unless you are aborting script execution using ``exit()`` or ``die()`` in your code.
Ahmad Anbar93c09872014-05-31 18:52:13 +0300221
Ahmad Anbar054bbe82014-06-01 12:20:08 +0300222 Example::
vlakoffa7812042014-10-24 11:27:31 +0200223
Ahmad Anbar93c09872014-05-31 18:52:13 +0300224 $response = array('status' => 'OK');
225
226 $this->output
Ahmad Anbar054bbe82014-06-01 12:20:08 +0300227 ->set_status_header(200)
228 ->set_content_type('application/json', 'utf-8')
229 ->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
230 ->_display();
231 exit;
Ahmad Anbar93c09872014-05-31 18:52:13 +0300232
kenjis87ed4022015-07-17 11:29:51 +0900233 .. note:: Calling this method manually without aborting script execution will result in duplicated output.