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