Home > Framework > CodeIgniter > How to create custom library file in CodeIgniter

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

4 Responses

  1. Pingback : core PHP file use as library in CakePHP - Anil Labs

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.