blob: 76197bdc186e5368236508384d0f7497cb959083 [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 Andreevc8f34852014-01-03 13:47:27 +020032.. 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 Andreevc8f34852014-01-03 13:47:27 +020044 .. method:: set_output($output)
Derek Jones8ede1a22011-10-05 13:34:52 -050045
Andrey Andreevc8f34852014-01-03 13:47:27 +020046 :param string $output: String to set the output to
47 :returns: object
Derek Jones8ede1a22011-10-05 13:34:52 -050048
Andrey Andreevc8f34852014-01-03 13:47:27 +020049 Permits you to manually set the final output string. Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -050050
Andrey Andreevc8f34852014-01-03 13:47:27 +020051 $this->output->set_output($data);
Derek Jones8ede1a22011-10-05 13:34:52 -050052
Andrey Andreevc8f34852014-01-03 13:47:27 +020053 .. important:: If you do set your output manually, it must be the last thing done
54 in the function you call it from. For example, if you build a page in one
55 of your controller functions, don't set the output until the end.
Andrey Andreev47b67332012-06-06 15:58:05 +030056
Andrey Andreevc8f34852014-01-03 13:47:27 +020057 .. method:: set_content_type($mime_type[, $charset = NULL])
Andrey Andreev47b67332012-06-06 15:58:05 +030058
Andrey Andreevc8f34852014-01-03 13:47:27 +020059 :param string $mime_type: MIME Type idenitifer string
60 :param string $charset: Character set
61 :returns: object
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070062
Andrey Andreevc8f34852014-01-03 13:47:27 +020063 Permits you to set the mime-type of your page so you can serve JSON data, JPEG's, XML, etc easily.
64 ::
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070065
Andrey Andreevc8f34852014-01-03 13:47:27 +020066 $this->output
67 ->set_content_type('application/json')
68 ->set_output(json_encode(array('foo' => 'bar')));
Andrey Andreev00adf1d2012-04-03 12:30:50 +030069
Andrey Andreevc8f34852014-01-03 13:47:27 +020070 $this->output
71 ->set_content_type('jpeg') // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php
72 ->set_output(file_get_contents('files/something.jpg'));
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070073
Andrey Andreevc8f34852014-01-03 13:47:27 +020074 .. important:: Make sure any non-mime string you pass to this method
75 exists in *application/config/mimes.php* or it will have no effect.
Andrey Andreevcc4b0032012-11-29 17:21:43 +020076
Andrey Andreevc8f34852014-01-03 13:47:27 +020077 You can also set the character set of the document, by passing a second argument::
Andrey Andreevcc4b0032012-11-29 17:21:43 +020078
Andrey Andreevc8f34852014-01-03 13:47:27 +020079 $this->output->set_content_type('css', 'utf-8');
Andrey Andreevcc4b0032012-11-29 17:21:43 +020080
Andrey Andreevc8f34852014-01-03 13:47:27 +020081 .. method:: get_content_type()
Andrey Andreevcc4b0032012-11-29 17:21:43 +020082
Andrey Andreevc8f34852014-01-03 13:47:27 +020083 :returns: string
Andrey Andreevcc4b0032012-11-29 17:21:43 +020084
Andrey Andreevc8f34852014-01-03 13:47:27 +020085 Returns the Content-Type HTTP header that's currently in use, excluding the character set value.
86 ::
Andrey Andreevcc4b0032012-11-29 17:21:43 +020087
Andrey Andreevc8f34852014-01-03 13:47:27 +020088 $mime = $this->output->get_content_type();
Andrey Andreevcc4b0032012-11-29 17:21:43 +020089
Andrey Andreevc8f34852014-01-03 13:47:27 +020090 .. note:: If not set, the default return value is 'text/html'.
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Andrey Andreevc8f34852014-01-03 13:47:27 +020092 .. method:: get_header($header)
Derek Jones8ede1a22011-10-05 13:34:52 -050093
Andrey Andreevc8f34852014-01-03 13:47:27 +020094 :param string $header: HTTP header name
95 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -050096
Andrey Andreevc8f34852014-01-03 13:47:27 +020097 Returns the requested HTTP header value, or NULL if the requested header is not set.
98 Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050099
Andrey Andreevc8f34852014-01-03 13:47:27 +0200100 $this->output->set_content_type('text/plain', 'UTF-8');
101 echo $this->output->get_header('content-type');
102 // Outputs: text/plain; charset=utf-8
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Andrey Andreevc8f34852014-01-03 13:47:27 +0200104 .. note:: The header name is compared in a case-insensitive manner.
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
Andrey Andreevc8f34852014-01-03 13:47:27 +0200106 .. note:: Raw headers sent via PHP's native ``header()`` function are also detected.
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
Andrey Andreevc8f34852014-01-03 13:47:27 +0200108 .. method:: get_output()
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
Andrey Andreevc8f34852014-01-03 13:47:27 +0200110 :returns: string
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Andrey Andreevc8f34852014-01-03 13:47:27 +0200112 Permits you to manually retrieve any output that has been sent for
113 storage in the output class. Usage example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Andrey Andreevc8f34852014-01-03 13:47:27 +0200115 $string = $this->output->get_output();
Derek Jones8ede1a22011-10-05 13:34:52 -0500116
Andrey Andreevc8f34852014-01-03 13:47:27 +0200117 Note that data will only be retrievable from this function if it has
118 been previously sent to the output class by one of the CodeIgniter
119 functions like ``$this->load->view()``.
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Andrey Andreevc8f34852014-01-03 13:47:27 +0200121 .. method:: append_output($output)
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Andrey Andreevc8f34852014-01-03 13:47:27 +0200123 :param string $output: Additional output data to append
124 :returns: object
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
Andrey Andreevc8f34852014-01-03 13:47:27 +0200126 Appends data onto the output string.
127 ::
Andrey Andreev956631d2012-10-06 20:43:47 +0300128
Andrey Andreevc8f34852014-01-03 13:47:27 +0200129 $this->output->append_output($data);
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
Andrey Andreevc8f34852014-01-03 13:47:27 +0200131 .. method:: set_header($header[, $replace = TRUE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
Andrey Andreevc8f34852014-01-03 13:47:27 +0200133 :param string $header: HTTP Header
134 :param bool $replace: Whether to replace the old header value, if it is already set
135 :returns: object
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
Andrey Andreevc8f34852014-01-03 13:47:27 +0200137 Permits you to manually set server headers, which the output class will
138 send for you when outputting the final rendered display. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500139
Andrey Andreevc8f34852014-01-03 13:47:27 +0200140 $this->output->set_header('HTTP/1.0 200 OK');
141 $this->output->set_header('HTTP/1.1 200 OK');
142 $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
143 $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
144 $this->output->set_header('Cache-Control: post-check=0, pre-check=0');
145 $this->output->set_header('Pragma: no-cache');
Derek Jones8ede1a22011-10-05 13:34:52 -0500146
Andrey Andreevc8f34852014-01-03 13:47:27 +0200147 .. method:: set_status_header([$code = 200[, $text = '']])
Derek Jones8ede1a22011-10-05 13:34:52 -0500148
Andrey Andreevc8f34852014-01-03 13:47:27 +0200149 :param int $code: HTTP status code
150 :param string $text: Optional message
151 :returns: object
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
Andrey Andreevc8f34852014-01-03 13:47:27 +0200153 Permits you to manually set a server status header. Example::
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
Andrey Andreevc8f34852014-01-03 13:47:27 +0200155 $this->output->set_status_header('401');
156 // Sets the header as: Unauthorized
Derek Jones8ede1a22011-10-05 13:34:52 -0500157
Andrey Andreevc8f34852014-01-03 13:47:27 +0200158 `See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for a full list of headers.
Derek Jones8ede1a22011-10-05 13:34:52 -0500159
Andrey Andreevc8f34852014-01-03 13:47:27 +0200160 .. note:: This method is an alias for :doc:`Common function <../general/common_functions>`
161 :func:`set_status_header()`.
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
Andrey Andreevc8f34852014-01-03 13:47:27 +0200163 .. method:: enable_profiler([$val = TRUE])
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Andrey Andreevc8f34852014-01-03 13:47:27 +0200165 :param bool $val: Whether to enable or disable the Profiler
166 :returns: object
Derek Jones8ede1a22011-10-05 13:34:52 -0500167
Andrey Andreevc8f34852014-01-03 13:47:27 +0200168 Permits you to enable/disable the :doc:`Profiler <../general/profiling>`, which will display benchmark
169 and other data at the bottom of your pages for debugging and optimization purposes.
Derek Jones8ede1a22011-10-05 13:34:52 -0500170
Andrey Andreevc8f34852014-01-03 13:47:27 +0200171 To enable the profiler place the following line anywhere within your
172 :doc:`Controller <../general/controllers>` methods::
173
174 $this->output->enable_profiler(TRUE);
175
176 When enabled a report will be generated and inserted at the bottom of your pages.
177
178 To disable the profiler you would use::
179
180 $this->output->enable_profiler(FALSE);
181
182 .. method:: set_profiler_sections($sections)
183
184 :param array $sections: Profiler sections
185 :returns: object
186
187 Permits you to enable/disable specific sections of the Profiler when it is enabled.
188 Please refer to the :doc:`Profiler <../general/profiling>` documentation for further information.
189
190 .. method:: cache($time)
191
192 :param int $time: Cache expiration time in seconds
193 :returns: object
194
195 Caches the current page for the specified amount of seconds.
196
197 For more information, please see the :doc:`caching documentation <../general/caching>`.