This posts explains about How to create custom library file in CodeIgniter, in earlier we know about how to remove index.php file from URLs. We have follow few steps to create custom library in CodeIgniter.

How to create custom library file in CodeIgniter by Anil Kumar Panigrahi
Step 1 :
Go to folder application -> libraries
Step 2 :
Create a PHP file with YourLibraryName_lib.php
Step 3 :
Starting of library file write below code
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author Anil Kumar Panigrahi
* @copyright Copyright (c) 2015, Anil Labs.
* @license
* @link http://www.anillabs.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Anil Labs core CodeIgniter class
*
* @package CodeIgniter
* @subpackage Libraries
* @category Anil Labs
* @author Anil Kumar Panigrahi
* @link http://www.anillabs.com
*/
Note: We can change description according to your requirement.
Step 4 :
Create library class with database connection and load the pre-defined libraries
var $CI;
public function __construct($params = array())
{
$this->CI =& get_instance();
$this->CI->load->helper('url');
$this->CI->config->item('base_url');
$this->CI->load->database();
}
Step 5 :
We can write your function to manipulate
$sql = "SELECT * FROM pages WHERE pageid = ?";
$results = $this->CI->db->query($sql, array($pageid));
$pageMetadata = $results->row();
$data['keywords'] = $pageMetadata->keywords;
$data['description'] = $pageMetadata->description;
$this->CI->load->view('templates/header',$data);
}
Step 6 :
Go to /application/config/autoload.php
Search for
and add your library file which you need to allow in complete application. If you don’t want to allow in complete application then we can write below code in single controller
Step 7 :
Once you load library file in above step, next calling library file in your controller method like below
Complete library file code
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package CodeIgniter
* @author Anil Kumar Panigrahi
* @copyright Copyright (c) 2015, Anil Labs.
* @license
* @link http://www.anillabs.com
* @since Version 1.0
* @filesource
*/
// ------------------------------------------------------------------------
/**
* Anil Labs core CodeIgniter class
*
* @package CodeIgniter
* @subpackage Libraries
* @category Anil Labs
* @author Anil Kumar Panigrahi
* @link http://www.anillabs.com
*/
class Anillabs_lib {
var $CI;
public function __construct($params = array())
{
$this->CI =& get_instance();
$this->CI->load->helper('url');
$this->CI->config->item('base_url');
$this->CI->load->database();
}
public function getMetadata($pageid){
$sql = "SELECT * FROM pages WHERE pageid = ?";
$results = $this->CI->db->query($sql, array($pageid));
$pageMetadata = $results->row();
$data['keywords'] = $pageMetadata->keywords;
$data['description'] = $pageMetadata->description;
$this->CI->load->view('templates/header',$data);
}
}
![]() |
I am writing this code based on CodeIgniter 2.2.1
Simple and easy to understand
thanks brother.
Thank you so much, your code helped to me. First time i created one library by the help of your code.
Pingback : core PHP file use as library in CakePHP - Anil Labs
in version 3.1.8, the format will be
$autoload[‘libraries’] = array(‘anillabs_lib’);