How to create custom library in CodeIgniter with demo
How to create custom library in CodeIgniter with demo. This posts explains How to create custom library file in CodeIgniter,create you own library in ci.When we use the term “Libraries” we are normally referring to the classes that are located in the libraries directory and described in the Class Reference of this user guide. In this case, however, we will instead describe how you can create your own libraries within your application/libraries directory in order to maintain separation between your local resources and the global framework resources.
As an added bonus, CodeIgniter permits your libraries to extend native classes if you simply need to add some functionality to an existing library. Or you can even replace native libraries just by placing identically named versions in your application/libraries folder.Codeigniter libraries means classes which located in libraries directory. so for create our own custom library in codeigniter we need to build classes and store class file in libraries directory full path of libraries directory will be “application/libraries”. It’s easy to create new/ custom library , replace native or extend native library in codeigniter.
Below in this tutorial we will discuss :-
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 and We can change description according to your requirement.
<?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 Phpcodehub * @copyright Copyright (c) 2015, Phpcodehub. * @license * @link Phpcodehub.php * @since Version 1.0 * @filesource */ // ------------------------------------------------------------------------ /** * Anil Labs core CodeIgniter class * * @package CodeIgniter * @subpackage Libraries * @category Phpcodehub * @author Phpcodehub * @link http://www.Phpcodehub.com */
Step 4 :
Create library class with database connection and load the pre-defined libraries
class phpcodehub_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 create 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
$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('phpcodehub_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 Phpcodehub * @copyright Copyright (c) 2015, Phpcodehub. * @license * @link Phpcodehub.php * @since Version 1.0 * @filesource */ // ------------------------------------------------------------------------ /** * Anil Labs core CodeIgniter class * * @package CodeIgniter * @subpackage Libraries * @category Phpcodehub * @author Phpcodehub * @link http://www.Phpcodehub.com */ class phpcodehub_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); } }