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)
  • Parameters :
  • $output (string) : String to set the output to
  • Returns : CI_Output instance
  • Returns type : CI_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:-

Codeigniter Output Class Library

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])
  • Parameters :
  • $mime_type (string) : MIME Type idenitifer string
  • $charset (string) : Character set
  • Returns : CI_Output instance
  • Returns type : CI_Output
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:-

Codeigniter Output Class Library
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:-

Codeigniter Output Class Library

4. Get content type.

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

get_content_type()
  • Parameters :
  • Returns : Content-Type string
  • Returns type : string
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:-

Codeigniter Output Class Library

5. Get header.

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

get_header($header)
  • Parameters :
  • $header (string) : HTTP header name
  • Returns : HTTP response header or NULL if not found
  • Returns type : Mixed
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:-

Codeigniter Output Class Library

6. Get output.

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

get_output()
  • Parameters :
  • Returns : Output string
  • Returns type : String

7. Append output.

This reference return append data in to output string.

append_output($output)
  • Parameters :
  • $output (string) : Additional output data to append
  • Returns : CI_Output instance
  • Returns type : CI_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])
  • Parameters :
  • $header (string) : HTTP response header
  • $replace (bool) : Whether to replace the old header value, if it is already set
  • Returns : CI_Output instance
  • Returns type : CI_Output
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 = '']])
  • Parameters :
  • $code (int) : HTTP status code
  • $text (string) : Optional message
  • Returns : CI_Output instance
  • Returns type : CI_Output
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])
  • Parameters :
  • $val (bool) : Whether to enable or disable the Profiler
  • Returns : CI_Output instance
  • Returns type : CI_Output
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)
  • Parameters :
  • $sections (array) : Profiler sections
  • Returns : CI_Output instance
  • Returns type : CI_Output

12. Cache.

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

cache($time)
  • Parameters :
  • $time (int) : Cache expiration time in minutes
  • Returns : CI_Output instance
  • Returns type : CI_Output

13. Display.

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

_display([$output = ''])
  • Parameters :
  • $output (string) : Output data override
  • Returns : void
  • Returns type : void
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:-

Codeigniter Output Class Library

Advertisements

Add Comment

📖 Read More