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 | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 32 | .. php: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 | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 44 | .. php:method:: set_output($output) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 45 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 46 | :param string $output: String to set the output to |
| 47 | :returns: CI_Output instance (method chaining) |
| 48 | :rtype: CI_Output |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 50 | Permits you to manually set the final output string. Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 51 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 52 | $this->output->set_output($data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 54 | .. important:: If you do set your output manually, it must be the last thing done |
| 55 | in the function you call it from. For example, if you build a page in one |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 56 | of your controller methods, don't set the output until the end. |
Andrey Andreev | 47b6733 | 2012-06-06 15:58:05 +0300 | [diff] [blame] | 57 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 58 | .. php:method:: set_content_type($mime_type[, $charset = NULL]) |
Andrey Andreev | 47b6733 | 2012-06-06 15:58:05 +0300 | [diff] [blame] | 59 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 60 | :param string $mime_type: MIME Type idenitifer string |
| 61 | :param string $charset: Character set |
| 62 | :returns: CI_Output instance (method chaining) |
| 63 | :rtype: CI_Output |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 64 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 65 | Permits you to set the mime-type of your page so you can serve JSON data, JPEG's, XML, etc easily. |
| 66 | :: |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 67 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 68 | $this->output |
| 69 | ->set_content_type('application/json') |
| 70 | ->set_output(json_encode(array('foo' => 'bar'))); |
Andrey Andreev | 00adf1d | 2012-04-03 12:30:50 +0300 | [diff] [blame] | 71 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 72 | $this->output |
| 73 | ->set_content_type('jpeg') // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php |
| 74 | ->set_output(file_get_contents('files/something.jpg')); |
Songpol Sripaoeiam | 52fe7bb | 2012-04-01 11:43:20 +0700 | [diff] [blame] | 75 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 76 | .. important:: Make sure any non-mime string you pass to this method |
| 77 | exists in *application/config/mimes.php* or it will have no effect. |
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 | 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] | 80 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 81 | $this->output->set_content_type('css', 'utf-8'); |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 82 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 83 | .. php:method:: get_content_type() |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 84 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 85 | :returns: Content-Type string |
| 86 | :rtype: string |
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 | Returns the Content-Type HTTP header that's currently in use, excluding the character set value. |
| 89 | :: |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 90 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 91 | $mime = $this->output->get_content_type(); |
Andrey Andreev | cc4b003 | 2012-11-29 17:21:43 +0200 | [diff] [blame] | 92 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 93 | .. note:: If not set, the default return value is 'text/html'. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 95 | .. php:method:: get_header($header) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 96 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 97 | :param string $header: HTTP header name |
| 98 | :returns: HTTP response header or NULL if not found |
| 99 | :rtype: mixed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 101 | Returns the requested HTTP header value, or NULL if the requested header is not set. |
| 102 | Example:: |
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 | $this->output->set_content_type('text/plain', 'UTF-8'); |
| 105 | echo $this->output->get_header('content-type'); |
| 106 | // Outputs: text/plain; charset=utf-8 |
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 | .. note:: The header name is compared in a case-insensitive manner. |
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 | .. 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] | 111 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 112 | .. php:method:: get_output() |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 113 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 114 | :returns: Output string |
| 115 | :rtype: string |
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 | Permits you to manually retrieve any output that has been sent for |
| 118 | storage in the output class. Usage example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 120 | $string = $this->output->get_output(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 122 | Note that data will only be retrievable from this function if it has |
| 123 | been previously sent to the output class by one of the CodeIgniter |
| 124 | functions like ``$this->load->view()``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 125 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 126 | .. php:method:: append_output($output) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 127 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 128 | :param string $output: Additional output data to append |
| 129 | :returns: CI_Output instance (method chaining) |
| 130 | :rtype: CI_Output |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 131 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 132 | Appends data onto the output string. |
| 133 | :: |
Andrey Andreev | 956631d | 2012-10-06 20:43:47 +0300 | [diff] [blame] | 134 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 135 | $this->output->append_output($data); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 136 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 137 | .. php:method:: set_header($header[, $replace = TRUE]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 139 | :param string $header: HTTP response header |
| 140 | :param bool $replace: Whether to replace the old header value, if it is already set |
| 141 | :returns: CI_Output instance (method chaining) |
| 142 | :rtype: CI_Output |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 144 | Permits you to manually set server headers, which the output class will |
| 145 | send for you when outputting the final rendered display. Example:: |
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 | $this->output->set_header('HTTP/1.0 200 OK'); |
| 148 | $this->output->set_header('HTTP/1.1 200 OK'); |
| 149 | $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT'); |
| 150 | $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate'); |
| 151 | $this->output->set_header('Cache-Control: post-check=0, pre-check=0'); |
| 152 | $this->output->set_header('Pragma: no-cache'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 153 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 154 | .. php:method:: set_status_header([$code = 200[, $text = '']]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 155 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 156 | :param int $code: HTTP status code |
| 157 | :param string $text: Optional message |
| 158 | :returns: CI_Output instance (method chaining) |
| 159 | :rtype: CI_Output |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 160 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 161 | Permits you to manually set a server status header. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 162 | |
kenjis | 87ed402 | 2015-07-17 11:29:51 +0900 | [diff] [blame^] | 163 | $this->output->set_status_header(401); |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 164 | // Sets the header as: Unauthorized |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 165 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 166 | `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] | 167 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 168 | .. note:: This method is an alias for :doc:`Common function <../general/common_functions>` |
| 169 | :func:`set_status_header()`. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 170 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 171 | .. php:method:: enable_profiler([$val = TRUE]) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 173 | :param bool $val: Whether to enable or disable the Profiler |
| 174 | :returns: CI_Output instance (method chaining) |
| 175 | :rtype: CI_Output |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 176 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 177 | Permits you to enable/disable the :doc:`Profiler <../general/profiling>`, which will display benchmark |
| 178 | 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] | 179 | |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 180 | To enable the profiler place the following line anywhere within your |
| 181 | :doc:`Controller <../general/controllers>` methods:: |
| 182 | |
| 183 | $this->output->enable_profiler(TRUE); |
| 184 | |
| 185 | When enabled a report will be generated and inserted at the bottom of your pages. |
| 186 | |
| 187 | To disable the profiler you would use:: |
| 188 | |
| 189 | $this->output->enable_profiler(FALSE); |
| 190 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 191 | .. php:method:: set_profiler_sections($sections) |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 192 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 193 | :param array $sections: Profiler sections |
| 194 | :returns: CI_Output instance (method chaining) |
| 195 | :rtype: CI_Output |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 196 | |
| 197 | Permits you to enable/disable specific sections of the Profiler when it is enabled. |
| 198 | Please refer to the :doc:`Profiler <../general/profiling>` documentation for further information. |
| 199 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 200 | .. php:method:: cache($time) |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 201 | |
Andrey Andreev | 28c2c97 | 2014-02-08 04:27:48 +0200 | [diff] [blame] | 202 | :param int $time: Cache expiration time in seconds |
| 203 | :returns: CI_Output instance (method chaining) |
| 204 | :rtype: CI_Output |
Andrey Andreev | c8f3485 | 2014-01-03 13:47:27 +0200 | [diff] [blame] | 205 | |
| 206 | Caches the current page for the specified amount of seconds. |
| 207 | |
Ahmad Anbar | 93c0987 | 2014-05-31 18:52:13 +0300 | [diff] [blame] | 208 | For more information, please see the :doc:`caching documentation <../general/caching>`. |
| 209 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 210 | .. php:method:: _display([$output = '']) |
Ahmad Anbar | 93c0987 | 2014-05-31 18:52:13 +0300 | [diff] [blame] | 211 | |
| 212 | :param string $output: Output data override |
| 213 | :returns: void |
| 214 | :rtype: void |
| 215 | |
Ahmad Anbar | 054bbe8 | 2014-06-01 12:20:08 +0300 | [diff] [blame] | 216 | Sends finalized output data to the browser along with any server headers. It also stops benchmark |
| 217 | timers. |
Ahmad Anbar | 93c0987 | 2014-05-31 18:52:13 +0300 | [diff] [blame] | 218 | |
Ahmad Anbar | e06c5c4 | 2014-06-01 12:22:12 +0300 | [diff] [blame] | 219 | .. note:: This method is called automatically at the end of script execution, you won't need to |
| 220 | call it manually unless you are aborting script execution using ``exit()`` or ``die()`` in your code. |
Ahmad Anbar | 93c0987 | 2014-05-31 18:52:13 +0300 | [diff] [blame] | 221 | |
Ahmad Anbar | 054bbe8 | 2014-06-01 12:20:08 +0300 | [diff] [blame] | 222 | Example:: |
vlakoff | a781204 | 2014-10-24 11:27:31 +0200 | [diff] [blame] | 223 | |
Ahmad Anbar | 93c0987 | 2014-05-31 18:52:13 +0300 | [diff] [blame] | 224 | $response = array('status' => 'OK'); |
| 225 | |
| 226 | $this->output |
Ahmad Anbar | 054bbe8 | 2014-06-01 12:20:08 +0300 | [diff] [blame] | 227 | ->set_status_header(200) |
| 228 | ->set_content_type('application/json', 'utf-8') |
| 229 | ->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) |
| 230 | ->_display(); |
| 231 | exit; |
Ahmad Anbar | 93c0987 | 2014-05-31 18:52:13 +0300 | [diff] [blame] | 232 | |
kenjis | 87ed402 | 2015-07-17 11:29:51 +0900 | [diff] [blame^] | 233 | .. note:: Calling this method manually without aborting script execution will result in duplicated output. |