blob: 2cf7c0854aab195831fa56494c8199ce3bdf2277 [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
52$this->output->get_output();
53=============================
54
55Permits you to manually retrieve any output that has been sent for
56storage in the output class. Usage example::
57
58 $string = $this->output->get_output();
59
60Note that data will only be retrievable from this function if it has
61been previously sent to the output class by one of the CodeIgniter
62functions like $this->load->view().
63
64$this->output->append_output();
65================================
66
67Appends data onto the output string. Usage example::
68
69 $this->output->append_output($data);
70
71$this->output->set_header();
72=============================
73
74Permits you to manually set server headers, which the output class will
75send for you when outputting the final rendered display. Example::
76
Derek Jones36be9692011-10-05 15:52:41 -050077 $this->output->set_header("HTTP/1.0 200 OK");
78 $this->output->set_header("HTTP/1.1 200 OK");
79 $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
80 $this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
81 $this->output->set_header("Cache-Control: post-check=0, pre-check=0");
82 $this->output->set_header("Pragma: no-cache");
Derek Jones8ede1a22011-10-05 13:34:52 -050083
84$this->output->set_status_header(code, 'text');
85=================================================
86
87Permits you to manually set a server status header. Example::
88
Derek Jones36be9692011-10-05 15:52:41 -050089 $this->output->set_status_header('401');
90 // Sets the header as: Unauthorized
Derek Jones8ede1a22011-10-05 13:34:52 -050091
92`See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for
93a full list of headers.
94
95$this->output->enable_profiler();
96==================================
97
98Permits you to enable/disable the
99:doc:`Profiler <../general/profiling>`, which will display benchmark
100and other data at the bottom of your pages for debugging and
101optimization purposes.
102
103To enable the profiler place the following function anywhere within your
104:doc:`Controller <../general/controllers>` functions::
105
106 $this->output->enable_profiler(TRUE);
107
108When enabled a report will be generated and inserted at the bottom of
109your pages.
110
111To disable the profiler you will use::
112
113 $this->output->enable_profiler(FALSE);
114
115$this->output->set_profiler_sections();
116=========================================
117
118Permits you to enable/disable specific sections of the Profiler when
119enabled. Please refer to the :doc:`Profiler <../general/profiling>`
120documentation for further information.
121
122$this->output->cache();
123=======================
124
125The CodeIgniter output library also controls caching. For more
126information, please see the :doc:`caching
127documentation <../general/caching>`.
128
129Parsing Execution Variables
130===========================
131
132CodeIgniter will parse the pseudo-variables {elapsed_time} and
133{memory_usage} in your output by default. To disable this, set the
134$parse_exec_vars class property to FALSE in your controller.
135::
136
137 $this->output->parse_exec_vars = FALSE;
138