How to create custom library file in CodeIgniter

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

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

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 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

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();
       
    }

Step 5 :

We can write your function to manipulate

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);
}

Step 6 :

Go to /application/config/autoload.php

Search for

$autoload['libraries']

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

$this->load->library('anillabs_lib');

Step 7 :

Once you load library file in above step, next calling library file in your controller method like below

$this->anillabs_lib->getMetadata(5);

Complete library file code

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 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);
       }
}
download link for How to create custom library file in CodeIgniter by Anil Kumar Panigrahi

I am writing this code based on CodeIgniter 2.2.1

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

3 Responses

  1. Vapes says:

    Simple and easy to understand
    thanks brother.

  2. Pradeep says:

    Thank you so much, your code helped to me. First time i created one library by the help of your code.

  1. December 7, 2017

    […] have requirement that use external file need to use in CakePHP application. Earlier post we learned how to create library files in CodeIgniter. Now we can learn in CakePHP. In below I will explain how to do with […]

Leave a Reply

Your email address will not be published. Required fields are marked *