How to Create Custom Library in CodeIgniter

yotube
0

Hi, today let's see how to create custom library in codeigniter. CodeIgniter offers a rich set of libraries and helpers for robust application development. In general, a library is a class file used to group similar functionalities into a single file. Now we can do the same with helper but the libraries have their own object to store relevant data.

Below I'll show you how to create your own class libraries in CI. You must store these library files inside the 'application/libraries' folder. This will keep them separate from the native libraries of the framework.

create custom library in codeigniter

CodeIgniter - Create Custom Library:

CodeIgniter is a flexible MVC framework and allows you to,

  1. Create new custom libraries.
  2. Extend existing libraries to include additional functionalities to them.
  3. Replace native libraries completely with your own version.

I'm going to show you an example where I create a custom library to export the database table as a json string.

STEP-1) Create a library named 'Mylib.php' inside the 'application/libraries' folder. Make sure the file name matches the class name.

Mylib.php


<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class Mylib
{
public function to_json($tblname)
{
$CI =& get_instance();
$CI->load->database();
$query = $CI->db->get($tblname);
return json_encode($query->result(), JSON_PRETTY_PRINT);
}
}
?>

In the 'Mylib' class, I have added a member function to_json() which takes up a database table name as a parameter, fetch and return the data as json.

STEP-2) Next we need a controller file to test the library. So create a controller 'TestController.php' within 'application/controllers' folder.

TestController.php


<?php if (! defined('BASEPATH')) exit('No direct script access allowed');

class TestController extends CI_Controller
{
function __construct()
{
parent::__construct();
}

function index()
{
// code
}
}
?>

STEP-3) Now load the library within the controller's index() function.


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

You can also auto load the library inside 'application/config/autoload.php' file. Just look for the line, $autoload['libraries'] and add the library name. Auto loading will make it available for the entire application.

STEP-4) Then call the to_json() function to export the database table as json.


$json = $this->mylib->to_json('customers');
print '<pre>' . $json . '</pre>';

Now running the 'TestController' will output the data from the given table as a json string on the browser.

This is just a simple example of what a custom library can do.

Extending a Native Library:

Sometimes you may want to include some additional functions to a native library. In such case you can just extend the library like this,


class MY_Email extends CI_Email
{

public function __construct()
{
parent::__construct();
}

public function my_function()
{
// code goes here
}
}

Please note that while extending a library you must set the prefix 'MY_' before the class name.

And you can change this prefix inside 'application/config/config.php' file. Just search for the below line and replace it with your choice of prefix.


$config['subclass_prefix'] = 'MY_';

To use my_function() in your application, load the 'email' library like you always do and access the function like this,


$this->load->library('email');
$this->email->my_function();

Replacing the Native Library:

In case you want to rewrite an entire native library, you can simply replace it with your own version. For example, to replace the 'Email' library, create a file 'Email.php' inside 'application/libraries' folder and declare the class like this,


class CI_Email
{
// code
}

And to use the class, you can simply load it on your controller or view like this,


$this->load->library('email');
Also Read:

That explains about creating custom libraries in codeigniter. I have also discussed about other ways of working with libraries. Just make sure to place all your library files inside 'application' > 'libraries' path and you are good to go. I hope you find this tutorial useful. Please share it on social media if you like it.

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top