×

CodeIgniter Output class

Output class

The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In some situations, it works transparently without the intervention of the user such as, when a Loader class is used to load a view file, it automatically passed control to the Output class. Further, it can be called automatically by the Codeigniter to load at the system execution, that’s you don’t need to load an output class in the application manually. Furthermore, it can also be useful for caching web pages.

Class References

There are various class references available in the output class:

  1. $parse_exec_vars: A parse variable is used to enable or disable the elapsed time and memory usage in pseudo-variables.

Syntax

parse_exex_vars = TRUE;

To disable the $parse variable in the CodeIgniter by setting the FALSE Property to the controller.

$this->output->parse_exec_vars = FALSE;

Create a controller file Output_controller.php and save it in application/controller/Output_controller.php. After that, write the following program in the controller file.

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function parse_data()
 {
 echo “<title> Tutorial and Example </title>”; 
 echo $this->output->parse_exec_vars = true;
 echo "parse variable is Enabled";
 }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/parse_data function; it shows the output, as shown below.

CodeIgniter Output class
  • set_ouput(): As the name defines, a set_output() function is used to set the final output string on the web page manually.

Syntax

$this->output->set_output( $string );

Create a controller file Output_controller.php and save it in application/controller/Output_controller.php. After that, write the following program in the controller file.

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function setoutput()
 {
     echo "<title> Tutorial and Example </title>";
 $string = "Welcome to the Tutorial and Example to learn the Codeigniter"; 
 $my_data = $this->output->set_output($string);
 }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/parse_data function; it shows the output, as shown below:

CodeIgniter Output class
  • set_content_type(): It is a function that allows you to set the mime type of the requested web page for serving the JSON data, XML, audio, JPEG, doc, etc. in the Codeigniter application.

Syntax

set_content_type ($mime_type [, $charset = NULL ]);

It has two parameters:

$mime_type: It defines the mime type such as JPG, docx, etc.

$charset: It defines the character set in the web page such as UTF – 8 and more.

Example 1. Create a controller file Output_controller.php and save it in application/controller/Output_controller.php. After that, write the following program in the controller file.

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function setcontent()
 {   
 $this->output
 ->set_content_type( 'application/json', 'utf-8' ) 
 ->set_output(json_encode( array('topic' => 'Codeigniter Tutorial')));
 }
 }
 ?>         

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/setcontent function; it shows the output, as shown below.

CodeIgniter Output class

Example 2. To shows an image on the web page.

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function setcontent()
 $this->output
 ->set_content_type('png') 
 ->set_output(file_get_contents( 'images/welcome.png' ));
 }
 }
 ?>         

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/setcontent function; it shows the output, as shown below.

CodeIgniter Output class
  • get_content_type(): A get_content_type() function is used to display the http type content (text/html, CSS, etc.) that are currently in use to the web application, excluding the character set value.

Syntax

$this->ouput->get_content_type():

Example:

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function setcontent()
 echo “<title> tutorial and Example </title>;
 $mime = $this->output->get_content_type();
 echo $mime; 
 } 
 } 
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/Get_type function; it shows the output, as shown below.

CodeIgniter Output class
  • get_header(): It is used to returns the requested HTTP header type and the set character value to the web pages, or if the requested header is not set, it returns the NULL value.

Syntax

get_header ($header); 

$header: It defines the HTTP header name.

Example:

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function content_type()
 {
 $this->output->set_content_type( ‘text/html’, ‘UTF-8’ ); 
 Echo $this->output->get_header( ‘content-type’ );
 }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/content_type function; it shows the output, as shown below.

CodeIgniter Output class
  • get_output(): As the name suggests, a get_output() function is used to manually fetch any output data that has been previously sent for the storage in the output class.

Syntax

$data = $this->output->get_output();

Example: Create a controller file Output_controller.php and save it in application/controller/Output_controller.php. After that, write the following program in the controller file.

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function fetch_data()
 {   
 echo “<title> Tutorial and Example </title>”; 
 $this->load->view (‘display’);
 $string = $this->output->get_output();
 echo $string;    
 }
 }
 ?>         

Create a view file display.php and save it in application/view/display.php. After that, write the following program in the controller file.

display.php

<h3> Welcome to the Tutorial and Example </h3>

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/fetch_data function; it shows the output, as shown below.

CodeIgniter Output class

Note: This function is only to retrieve those data that are already sent to the output class by the following function, such as $this-> load-> view().

  • append_output(): An append_output() function is used to append additional data to the end of the output string.

Syntax

append_output ( $output); 

$output: It is a variable that contains an additional data string to append to the original output.

Example:

$string = “hello”;
$data = $this->output->append_output ($string);
print_r ($data); 
  • set_header(): It allows you to manually set the server header to display or render the final output or web page in the Codeigniter application.

Syntax

set_header ($header [, $replace = TRUE ] );

$header: It requires an HTTP response header.

$replace (bool): It uses a Boolean value to replace the old header value that already exists to the final output.

Example: To set the header in a finalized web page of the Codeigniter application:

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function set_header()
 { 
 $this->output->set_header('HTTP/1.0 400 OK', false);
 $this->output->set_header('HTTP/1.5 105 '); 
 $this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
 echo "Header has been successfully saved";
 }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/set_header function; it shows the output, as shown below.

CodeIgniter Output class
  • set_status_header(): It is used to manually set the server header status to the final web page.

Syntax

set_status_header ( [$code = 205 [, $text = ‘’] ] ); 

It has two parameters:

$code: It takes an integer value to determine the HTTP status code.

$text: In this parameter, you can pass the optional message.

Example: write a program to set header status in the localhost server.

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function status()
 { 
 $code = 502; 
 $text = "tutorialandexample.com";
 $this->output->set_status_header($code, $text);
 }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/status function; it shows the output, as shown below.

CodeIgniter Output class
  1. enable_profiler(): As the name defines, an enable_profiler() function is used to enable or disable the profiler to display the benchmark or other data functionality for debugging and optimization purposes.

Syntax

enable_profiler ( [ $val = TRUE ] );

$val (bool): It uses a Boolean value to enable or disable the profiler.

Example: Create an Output_controller.php file at the application/controller/  

directory and then write the following program.

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function enable_profiler()
 { 
 echo "<title> Tutorial and Eaxmple </title>";
 $this->output->enable_profiler(true); 
 }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/enable_profiler function; it shows the output, as shown below.

CodeIgniter Output class
  1. set_profiler_sections(): A set_profiler_sections() function is used to manually enabled or disabled the specific sections of the profiler.

Syntax

$sections: It defines the specific sections to enable or disable in the profiler.

Example: $this->output ->set_profiler_sections( $profiler_sections);

  1. cache(): A cache () function is used to store the current web page for a specified time.

Syntax

cache ($time);

$time: This field contains the cache expiration time in minutes.  

Example: Create a controller file Output_controller.php and save it in application/controller/Output_controller.php. After that, write the following program in the controller file.

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
  public function cache_time()
 { 
 echo "<title> Tutorial and Eaxmple </title>"; 
 $int = 10;
 $this->output->cache($int);
 echo "Cache is saved for " .$int. " minutes";
 } 
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/cache_time function; it shows the output, as shown below.

CodeIgniter Output class
  1. _display(): A display () function is used to send the final output data to a browser with any server header. It is also used to stop the benchmarks time in the output class.

Syntax

_display ( [ $output = ‘’ ] );

$output: It contains an output data string to override.

Example: Create a Output_controller.php file inside the application/controller/ 

directory and then write the following program.

Output_controller.php

<?php
 defined('BASEPATH') OR exit( 'No direct script access allowed');
 class Output_controller extends CI_Controller 
 {
 public function show()
 {
 $response = array('condition' => 'Good'); 
 $this->output
         ->set_status_header(403)
         ->set_content_type('application/json', 'utf-8')
         ->set_output(json_encode($response, JSON_PRETTY_PRINT |    JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)  
   ->_display();
 exit;
 }
 }
 ?> 

To run the program in the localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Output_controller/show function; it shows the output, as shown below.

CodeIgniter Output class

Related Topics

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

CodeIgniter ZIP Encoding Class

ZIP Encoding Class The CodeIgniter provides a Zip encoding class that are used to compress a large file into a Zip archives. And this archives files can be downloaded either to your desktop or...

5 minutes read.

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

Form Validation Library Codeigniter

Form Validation Library The Codeigniter provides a form validation library that contains predefined rules to validate the form fields. It also helps in optimizing the form validation code, which reduces the effort and time...

31 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

CodeIgniter CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

3 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

1 minute read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

CodeIgniter Helpers

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

CodeIgniter HTML Helper

The HTML helper function is used in CodeIgniter to provide various functionality, such as headings, images, links, list tags, etc. Loading the HTML Helper Before using the html helper function, you must load the...

7 minutes read.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

6 minutes read.

CodeIgniter User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

4 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.