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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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

1
2
3
4
5
6
7
8
9
10
11
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

1
2
3
4
5
6
7
8
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

1
$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

1
$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

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

Complete library file code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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 Comments

Vapes · April 4, 2015 at 12:00 pm

Simple and easy to understand
thanks brother.

Pradeep · May 29, 2017 at 1:49 pm

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

Raj Keshwani · October 13, 2018 at 1:42 pm

in version 3.1.8, the format will be
$autoload[‘libraries’] = array(‘anillabs_lib’);

core PHP file use as library in CakePHP - Anil Labs · December 7, 2017 at 10:50 am

[…] 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

Avatar placeholder

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