Tutorialsplane

Codeigniter Output Class Library


Codeigniter Output Class Library – This library provide various functions that are used to send the finalized web page to the requesting browser. When we use the loader class to load a view file. It automatically passed to the output class. This class is automatically loaded by the system that’s why no need to load manually. It also responsible for caching your web page. Here in this tutorial, we are going to explain how to use Output class library.


Codeigniter output class library

Let us understand how output class library works in codeigniter with examples.

Class reference:-

There are following references available in output class library. Now we will explain.

1. parse var.

This reference is used to enalble/disable of elapsed time and memomory usage in pseudo variables.

parse_exec_vars = TRUE;
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class output_controller extends CI_Controller 
{
public function parse()
{
echo $this-?>output->parse_exec_vars = true;
}
}
?>

2. Set output.

This function gives permit to set final output string.

set_output($output)
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class output_controller extends CI_Controller 
{
public function setoutput()
{
$data = "Welcome to tutorialsplane";
$this-?>output->set_output($data);
}
}
?>

Output will be like this:-

3. Set content type.

This function also give permit you to set the mime-type of your page and serve JSON data, jpeg, xml.

set_content_type($mime_type[$charset = NULL])
Example 1.
<?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')
->set_output(json_encode(array('SolidCoupon' => 'Tutorialsplane')));
}	
}
?>

Output will be like this:-

Example 2.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class output_controller extends CI_Controller 
{
public function setcontent()
{
$this-?>output
->set_content_type('jpeg') 
->set_output(file_get_contents('uploads/Hydrangeas.jpg'));
}	
}
?>

Output will be like this:-

4. Get content type.

This function is used to return content type HTTP header which is currently in use. .

get_content_type()
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class output_controller extends CI_Controller 
{
public function getcontent()
{
$mime = $this-?>output->get_content_type();
echo $mime;
}	
}
?>

Output will be like this:-

5. Get header.

This function can be used to return the requested http header value.

get_header($header)
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class output_controller extends CI_Controller 
{
public function getheader()
{
$this-?>output->set_content_type('Tutorialsplane', 'UTF-8');
echo $this->output->get_header('content-type');
}	
}
?>

Output will be like this:-

6. Get output.

This reference permits you to transitive any output has been storage in output class.

get_output()

7. Append output.

This reference return append data in to output string.

append_output($output)
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class output_controller extends CI_Controller 
{
public function appendoutput()
{
$data = "abcd";
$string = $this-?>output->append_output($data);
}		
}
?>

Output will be like this:-

8. Set header.

This reference permits you to set server header.

set_header($header[$replace = TRUE])
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class output_controller extends CI_Controller 
{
public function appendoutput()
{
$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').' 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');
}		
}
?>

9. Set status header.

This function is used to set a server status header.

set_status_header([$code = 200[$text = '']])
Example.
$this->output->set_status_header(401);

10. Enable profiler.

This function is used to enable/disable the profiler. which will display benchmark and other data at the bottom of your page for debugging.

enable_profiler([$val = TRUE])
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class output_controller extends CI_Controller 
{
public function enableprofiler()
{
$this-?>output->enable_profiler(true);
}
}
?>

Output will be like this:-

11. Set profiler section.

This function is used to enable/disable specific section of the profiler.

set_profiler_sections($sections)

12. Cache.

This function is used to enable/disable caches the current page for the specific amount of times.

cache($time)

13. Display.

It sends output data to the browser with any server header. It also stop benchmark timers.

_display([$output = ''])
Example.
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class output_controller extends CI_Controller 
{
public function datadisplay()
{
$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;
}
}
?>

Output will be like this:-

Models

Connect Database

Helpers

Libraries

Helper Reference

Library Reference

Database Reference