blob: baceaae7bfdc82c6e5382ab0e4dfc41445b79812 [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
Songpol Sripaoeiam614db072012-04-03 01:29:28 +070052$this->output->get_content_type();
Songpol Sripaoeiamb9667012012-04-01 17:13:44 +070053==========================================
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070054
Andrey Andreev00adf1d2012-04-03 12:30:50 +030055Returns the Content-Type HTTP header that's currently in use.
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070056
Andrey Andreev00adf1d2012-04-03 12:30:50 +030057 $mime = $this->output->get_content_type();
58
59.. note:: If not set, the default return value is 'text/html'.
Songpol Sripaoeiam52fe7bb2012-04-01 11:43:20 +070060
Derek Jones8ede1a22011-10-05 13:34:52 -050061$this->output->get_output();
62=============================
63
64Permits you to manually retrieve any output that has been sent for
65storage in the output class. Usage example::
66
67 $string = $this->output->get_output();
68
69Note that data will only be retrievable from this function if it has
70been previously sent to the output class by one of the CodeIgniter
71functions like $this->load->view().
72
73$this->output->append_output();
74================================
75
76Appends data onto the output string. Usage example::
77
78 $this->output->append_output($data);
79
80$this->output->set_header();
81=============================
82
83Permits you to manually set server headers, which the output class will
84send for you when outputting the final rendered display. Example::
85
Derek Jones36be9692011-10-05 15:52:41 -050086 $this->output->set_header("HTTP/1.0 200 OK");
87 $this->output->set_header("HTTP/1.1 200 OK");
88 $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
89 $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
90 $this->output->set_header("Cache-Control: post-check=0, pre-check=0");
91 $this->output->set_header("Pragma: no-cache");
Derek Jones8ede1a22011-10-05 13:34:52 -050092
93$this->output->set_status_header(code, 'text');
94=================================================
95
96Permits you to manually set a server status header. Example::
97
Derek Jones36be9692011-10-05 15:52:41 -050098 $this->output->set_status_header('401');
99 // Sets the header as: Unauthorized
Derek Jones8ede1a22011-10-05 13:34:52 -0500100
101`See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for
102a full list of headers.
103
104$this->output->enable_profiler();
105==================================
106
107Permits you to enable/disable the
108:doc:`Profiler <../general/profiling>`, which will display benchmark
109and other data at the bottom of your pages for debugging and
110optimization purposes.
111
112To enable the profiler place the following function anywhere within your
113:doc:`Controller <../general/controllers>` functions::
114
115 $this->output->enable_profiler(TRUE);
116
117When enabled a report will be generated and inserted at the bottom of
118your pages.
119
120To disable the profiler you will use::
121
122 $this->output->enable_profiler(FALSE);
123
124$this->output->set_profiler_sections();
125=========================================
126
127Permits you to enable/disable specific sections of the Profiler when
128enabled. Please refer to the :doc:`Profiler <../general/profiling>`
129documentation for further information.
130
131$this->output->cache();
132=======================
133
134The CodeIgniter output library also controls caching. For more
135information, please see the :doc:`caching
136documentation <../general/caching>`.
137
138Parsing Execution Variables
139===========================
140
141CodeIgniter will parse the pseudo-variables {elapsed_time} and
142{memory_usage} in your output by default. To disable this, set the
143$parse_exec_vars class property to FALSE in your controller.
144::
145
146 $this->output->parse_exec_vars = FALSE;
147