Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############ |
| 2 | Output Class |
| 3 | ############ |
| 4 | |
| 5 | The Output class is a small class with one main function: To send the |
| 6 | finalized web page to the requesting browser. It is also responsible for |
| 7 | :doc:`caching <../general/caching>` your web pages, if you use that |
| 8 | feature. |
| 9 | |
| 10 | .. note:: This class is initialized automatically by the system so there |
| 11 | is no need to do it manually. |
| 12 | |
| 13 | Under normal circumstances you won't even notice the Output class since |
| 14 | it works transparently without your intervention. For example, when you |
| 15 | use the :doc:`Loader <../libraries/loader>` class to load a view file, |
| 16 | it's automatically passed to the Output class, which will be called |
| 17 | automatically by CodeIgniter at the end of system execution. It is |
| 18 | possible, however, for you to manually intervene with the output if you |
| 19 | need to, using either of the two following functions: |
| 20 | |
| 21 | $this->output->set_output(); |
| 22 | ============================= |
| 23 | |
| 24 | Permits 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 | |
| 36 | Permits you to set the mime-type of your page so you can serve JSON |
| 37 | data, 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 Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 52 | $this->output->get_current_content_type(); |
Songpol Sripaoeiam | b966701 | 2012-04-01 17:13:44 +0700 | [diff] [blame] | 53 | ========================================== |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 54 | |
| 55 | Get the current mime-type of your page and return 'text/html' by default. |
| 56 | |
| 57 | $this->output->get_current_content_type(); |
| 58 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | $this->output->get_output(); |
| 60 | ============================= |
| 61 | |
| 62 | Permits you to manually retrieve any output that has been sent for |
| 63 | storage in the output class. Usage example:: |
| 64 | |
| 65 | $string = $this->output->get_output(); |
| 66 | |
| 67 | Note that data will only be retrievable from this function if it has |
| 68 | been previously sent to the output class by one of the CodeIgniter |
| 69 | functions like $this->load->view(). |
| 70 | |
| 71 | $this->output->append_output(); |
| 72 | ================================ |
| 73 | |
| 74 | Appends data onto the output string. Usage example:: |
| 75 | |
| 76 | $this->output->append_output($data); |
| 77 | |
| 78 | $this->output->set_header(); |
| 79 | ============================= |
| 80 | |
| 81 | Permits you to manually set server headers, which the output class will |
| 82 | send for you when outputting the final rendered display. Example:: |
| 83 | |
Derek Jones | 36be969 | 2011-10-05 15:52:41 -0500 | [diff] [blame] | 84 | $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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 90 | |
| 91 | $this->output->set_status_header(code, 'text'); |
| 92 | ================================================= |
| 93 | |
| 94 | Permits you to manually set a server status header. Example:: |
| 95 | |
Derek Jones | 36be969 | 2011-10-05 15:52:41 -0500 | [diff] [blame] | 96 | $this->output->set_status_header('401'); |
| 97 | // Sets the header as: Unauthorized |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | |
| 99 | `See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for |
| 100 | a full list of headers. |
| 101 | |
| 102 | $this->output->enable_profiler(); |
| 103 | ================================== |
| 104 | |
| 105 | Permits you to enable/disable the |
| 106 | :doc:`Profiler <../general/profiling>`, which will display benchmark |
| 107 | and other data at the bottom of your pages for debugging and |
| 108 | optimization purposes. |
| 109 | |
| 110 | To enable the profiler place the following function anywhere within your |
| 111 | :doc:`Controller <../general/controllers>` functions:: |
| 112 | |
| 113 | $this->output->enable_profiler(TRUE); |
| 114 | |
| 115 | When enabled a report will be generated and inserted at the bottom of |
| 116 | your pages. |
| 117 | |
| 118 | To disable the profiler you will use:: |
| 119 | |
| 120 | $this->output->enable_profiler(FALSE); |
| 121 | |
| 122 | $this->output->set_profiler_sections(); |
| 123 | ========================================= |
| 124 | |
| 125 | Permits you to enable/disable specific sections of the Profiler when |
| 126 | enabled. Please refer to the :doc:`Profiler <../general/profiling>` |
| 127 | documentation for further information. |
| 128 | |
| 129 | $this->output->cache(); |
| 130 | ======================= |
| 131 | |
| 132 | The CodeIgniter output library also controls caching. For more |
| 133 | information, please see the :doc:`caching |
| 134 | documentation <../general/caching>`. |
| 135 | |
| 136 | Parsing Execution Variables |
| 137 | =========================== |
| 138 | |
| 139 | CodeIgniter 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 | |