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