Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############ |
| 2 | Output Class |
| 3 | ############ |
| 4 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 5 | The Output class is a core class with one main function: To send the |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 19 | need to. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 21 | .. contents:: |
| 22 | :local: |
| 23 | |
| 24 | .. raw:: html |
| 25 | |
| 26 | <div class="custom-index container"></div> |
| 27 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 28 | *************** |
| 29 | Class Reference |
| 30 | *************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 31 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 32 | .. class:: CI_Output |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 34 | .. attribute:: $parse_exec_vars = TRUE; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 35 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 36 | Enables/disables parsing of the {elapsed_time} and {memory_usage} pseudo-variables. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 37 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 38 | CodeIgniter will parse those tokens in your output by default. To disable this, set |
| 39 | this property to FALSE in your controller. |
| 40 | :: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 42 | $this->output->parse_exec_vars = FALSE; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 44 | .. method:: set_output($output) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 45 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 46 | :param string $output: String to set the output to |
| 47 | :returns: object |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 49 | Permits you to manually set the final output string. Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 50 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 51 | $this->output->set_output($data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 53 | .. important:: If you do set your output manually, it must be the last thing done |
| 54 | in the function you call it from. For example, if you build a page in one |
| 55 | of your controller functions, don't set the output until the end. |
Andrey Andreev | 47b6733 | 2012-06-06 15:58:05 +0300 | [diff] [blame] | 56 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 57 | .. method:: set_content_type($mime_type[, $charset = NULL]) |
Andrey Andreev | 47b6733 | 2012-06-06 15:58:05 +0300 | [diff] [blame] | 58 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 59 | :param string $mime_type: MIME Type idenitifer string |
| 60 | :param string $charset: Character set |
| 61 | :returns: object |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 62 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 63 | Permits you to set the mime-type of your page so you can serve JSON data, JPEG's, XML, etc easily. |
| 64 | :: |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 65 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 66 | $this->output |
| 67 | ->set_content_type('application/json') |
| 68 | ->set_output(json_encode(array('foo' => 'bar'))); |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 69 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 70 | $this->output |
| 71 | ->set_content_type('jpeg') // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php |
| 72 | ->set_output(file_get_contents('files/something.jpg')); |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 73 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 74 | .. important:: Make sure any non-mime string you pass to this method |
| 75 | exists in *application/config/mimes.php* or it will have no effect. |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 76 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 77 | You can also set the character set of the document, by passing a second argument:: |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 78 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 79 | $this->output->set_content_type('css', 'utf-8'); |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 80 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 81 | .. method:: get_content_type() |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 82 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 83 | :returns: string |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 84 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 85 | Returns the Content-Type HTTP header that's currently in use, excluding the character set value. |
| 86 | :: |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 87 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 88 | $mime = $this->output->get_content_type(); |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 89 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 90 | .. note:: If not set, the default return value is 'text/html'. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 92 | .. method:: get_header($header) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 93 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 94 | :param string $header: HTTP header name |
| 95 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 96 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 97 | Returns the requested HTTP header value, or NULL if the requested header is not set. |
| 98 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 99 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 100 | $this->output->set_content_type('text/plain', 'UTF-8'); |
| 101 | echo $this->output->get_header('content-type'); |
| 102 | // Outputs: text/plain; charset=utf-8 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 104 | .. note:: The header name is compared in a case-insensitive manner. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 105 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 106 | .. note:: Raw headers sent via PHP's native ``header()`` function are also detected. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 108 | .. method:: get_output() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 109 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 110 | :returns: string |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 111 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 112 | Permits you to manually retrieve any output that has been sent for |
| 113 | storage in the output class. Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 115 | $string = $this->output->get_output(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 116 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 117 | Note that data will only be retrievable from this function if it has |
| 118 | been previously sent to the output class by one of the CodeIgniter |
| 119 | functions like ``$this->load->view()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 121 | .. method:: append_output($output) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 122 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 123 | :param string $output: Additional output data to append |
| 124 | :returns: object |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 126 | Appends data onto the output string. |
| 127 | :: |
Andrey Andreev | 956631d | 2012-10-06 20:43:47 +0300 | [diff] [blame] | 128 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 129 | $this->output->append_output($data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 131 | .. method:: set_header($header[, $replace = TRUE]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 133 | :param string $header: HTTP Header |
| 134 | :param bool $replace: Whether to replace the old header value, if it is already set |
| 135 | :returns: object |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 136 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 137 | Permits you to manually set server headers, which the output class will |
| 138 | send for you when outputting the final rendered display. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 140 | $this->output->set_header('HTTP/1.0 200 OK'); |
| 141 | $this->output->set_header('HTTP/1.1 200 OK'); |
| 142 | $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT'); |
| 143 | $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate'); |
| 144 | $this->output->set_header('Cache-Control: post-check=0, pre-check=0'); |
| 145 | $this->output->set_header('Pragma: no-cache'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 146 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 147 | .. method:: set_status_header([$code = 200[, $text = '']]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 148 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 149 | :param int $code: HTTP status code |
| 150 | :param string $text: Optional message |
| 151 | :returns: object |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 153 | Permits you to manually set a server status header. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 154 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 155 | $this->output->set_status_header('401'); |
| 156 | // Sets the header as: Unauthorized |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 157 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 158 | `See here <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html>`_ for a full list of headers. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 159 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 160 | .. note:: This method is an alias for :doc:`Common function <../general/common_functions>` |
| 161 | :func:`set_status_header()`. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 163 | .. method:: enable_profiler([$val = TRUE]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 164 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 165 | :param bool $val: Whether to enable or disable the Profiler |
| 166 | :returns: object |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 167 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 168 | Permits you to enable/disable the :doc:`Profiler <../general/profiling>`, which will display benchmark |
| 169 | and other data at the bottom of your pages for debugging and optimization purposes. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 171 | To enable the profiler place the following line anywhere within your |
| 172 | :doc:`Controller <../general/controllers>` methods:: |
| 173 | |
| 174 | $this->output->enable_profiler(TRUE); |
| 175 | |
| 176 | When enabled a report will be generated and inserted at the bottom of your pages. |
| 177 | |
| 178 | To disable the profiler you would use:: |
| 179 | |
| 180 | $this->output->enable_profiler(FALSE); |
| 181 | |
| 182 | .. method:: set_profiler_sections($sections) |
| 183 | |
| 184 | :param array $sections: Profiler sections |
| 185 | :returns: object |
| 186 | |
| 187 | Permits you to enable/disable specific sections of the Profiler when it is enabled. |
| 188 | Please refer to the :doc:`Profiler <../general/profiling>` documentation for further information. |
| 189 | |
| 190 | .. method:: cache($time) |
| 191 | |
| 192 | :param int $time: Cache expiration time in seconds |
| 193 | :returns: object |
| 194 | |
| 195 | Caches the current page for the specified amount of seconds. |
| 196 | |
| 197 | For more information, please see the :doc:`caching documentation <../general/caching>`. |