Output Class¶
The Output class is a core class with one main function: To send the finalized web page to the requesting browser. It is also responsible for caching your web pages, if you use that feature.
Catatan
This class is initialized automatically by the system so there is no need to do it manually.
Under normal circumstances you won’t even notice the Output class since it works transparently without your intervention. For example, when you use the Loader class to load a view file, it’s automatically passed to the Output class, which will be called automatically by CodeIgniter at the end of system execution. It is possible, however, for you to manually intervene with the output if you need to.
Class Reference¶
-
class
CI_Output
¶ -
$parse_exec_vars = TRUE;
Enables/disables parsing of the {elapsed_time} and {memory_usage} pseudo-variables.
CodeIgniter will parse those tokens in your output by default. To disable this, set this property to FALSE in your controller.
$this->output->parse_exec_vars = FALSE;
-
set_output
($output)¶ Parameter: - $output (string) – String to set the output to
Kembali: CI_Output instance (method chaining)
Return type: CI_Output
Permits you to manually set the final output string. Usage example:
$this->output->set_output($data);
Penting
If you do set your output manually, it must be the last thing done in the function you call it from. For example, if you build a page in one of your controller methods, don’t set the output until the end.
-
set_content_type
($mime_type[, $charset = NULL])¶ Parameter: - $mime_type (string) – MIME Type idenitifer string
- $charset (string) – Character set
Kembali: CI_Output instance (method chaining)
Return type: CI_Output
Permits you to set the mime-type of your page so you can serve JSON data, JPEG’s, XML, etc easily.
$this->output ->set_content_type('application/json') ->set_output(json_encode(array('foo' => 'bar'))); $this->output ->set_content_type('jpeg') // You could also use ".jpeg" which will have the full stop removed before looking in config/mimes.php ->set_output(file_get_contents('files/something.jpg'));
Penting
Make sure any non-mime string you pass to this method exists in application/config/mimes.php or it will have no effect.
You can also set the character set of the document, by passing a second argument:
$this->output->set_content_type('css', 'utf-8');
-
get_content_type
()¶ Kembali: Content-Type string Return type: string Returns the Content-Type HTTP header that’s currently in use, excluding the character set value.
$mime = $this->output->get_content_type();
Catatan
If not set, the default return value is ‘text/html’.
-
get_header
($header)¶ Parameter: - $header (string) – HTTP header name
Kembali: HTTP response header or NULL if not found
Return type: mixed
Returns the requested HTTP header value, or NULL if the requested header is not set. Example:
$this->output->set_content_type('text/plain', 'UTF-8'); echo $this->output->get_header('content-type'); // Outputs: text/plain; charset=utf-8
Catatan
The header name is compared in a case-insensitive manner.
Catatan
Raw headers sent via PHP’s native
header()
function are also detected.
-
get_output
()¶ Kembali: Output string Return type: string Permits you to manually retrieve any output that has been sent for storage in the output class. Usage example:
$string = $this->output->get_output();
Note that data will only be retrievable from this function if it has been previously sent to the output class by one of the CodeIgniter functions like
$this->load->view()
.
-
append_output
($output)¶ Parameter: - $output (string) – Additional output data to append
Kembali: CI_Output instance (method chaining)
Return type: CI_Output
Appends data onto the output string.
$this->output->append_output($data);
-
set_header
($header[, $replace = TRUE])¶ Parameter: - $header (string) – HTTP response header
- $replace (bool) – Whether to replace the old header value, if it is already set
Kembali: CI_Output instance (method chaining)
Return type: CI_Output
Permits you to manually set server headers, which the output class will send for you when outputting the final rendered display. Example:
$this->output->set_header('HTTP/1.0 200 OK'); $this->output->set_header('HTTP/1.1 200 OK'); $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT'); $this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate'); $this->output->set_header('Cache-Control: post-check=0, pre-check=0'); $this->output->set_header('Pragma: no-cache');
-
set_status_header
([$code = 200[, $text = '']])¶ Parameter: - $code (int) – HTTP status code
- $text (string) – Optional message
Kembali: CI_Output instance (method chaining)
Return type: CI_Output
Permits you to manually set a server status header. Example:
$this->output->set_status_header(401); // Sets the header as: Unauthorized
See here for a full list of headers.
Catatan
This method is an alias for Common function
set_status_header()
.
-
enable_profiler
([$val = TRUE])¶ Parameter: - $val (bool) – Whether to enable or disable the Profiler
Kembali: CI_Output instance (method chaining)
Return type: CI_Output
Permits you to enable/disable the Profiler, which will display benchmark and other data at the bottom of your pages for debugging and optimization purposes.
To enable the profiler place the following line anywhere within your Controller methods:
$this->output->enable_profiler(TRUE);
When enabled a report will be generated and inserted at the bottom of your pages.
To disable the profiler you would use:
$this->output->enable_profiler(FALSE);
-
set_profiler_sections
($sections)¶ Parameter: - $sections (array) – Profiler sections
Kembali: CI_Output instance (method chaining)
Return type: CI_Output
Permits you to enable/disable specific sections of the Profiler when it is enabled. Please refer to the Profiler documentation for further information.
-
cache
($time)¶ Parameter: - $time (int) – Cache expiration time in seconds
Kembali: CI_Output instance (method chaining)
Return type: CI_Output
Caches the current page for the specified amount of seconds.
For more information, please see the caching documentation.
-
_display
([$output = ''])¶ Parameter: - $output (string) – Output data override
Kembali: void
Return type: void
Sends finalized output data to the browser along with any server headers. It also stops benchmark timers.
Catatan
This method is called automatically at the end of script execution, you won’t need to call it manually unless you are aborting script execution using
exit()
ordie()
in your code.Example:
$response = array('status' => 'OK'); $this->output ->set_status_header(200) ->set_content_type('application/json', 'utf-8') ->set_output(json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)) ->_display(); exit;
Catatan
Calling this method manually without aborting script execution will result in duplicated output.
-