CodeIgniter library

CodeIgniter library:

A CodeIgniter library is simply a class file that abstracts functionality into easy-to-use functions that take much of the strain off the developer. Take the Database Library as an example. It contains many functions that make the creation of complex SQL queries very easy; it also makes queries much more readable.
In CodeIgniter, project-specific libraries are located in the system/application/libraries/ folder and all of the CodeIgniter core libraries are located in the system/libraries/ folder

What do libraries do?

Libraries abstract out functionality for developers and make it easy to re-use code. CodeIgniter comes with many core libraries that provide ways to code your applications much faster than without libraries, removing much of the unnecessary code from your applications, and taking the strain off the developer.
There are many types of libraries included with CodeIgniter, and much more released by community members.
Every library has a different set of collected functions, but all work to make coding applications much simpler than they would be if you were not using a framework.

Loading Libraries:

You can load any library by using load method within a controller.  For example, you have Mycontroller.php

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

class Mycontroller extends CI_Controller {

 function __construct()
 {
 parent::__construct();
 //$this->load->library('class_name');
 $this->load->library('form_validation');
 }

Once initialized you can use it as per guidelines of the corresponding library.

Additionally, multiple libraries can be loaded at the same time by passing an array of libraries to the load method.

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

class Mycontroller extends CI_Controller {

 function __construct()
 {
 parent::__construct();
 //$this->load->library(array('class1','class2','class3'));
 $this->load->library(array('email', 'table'));
 }

If your library entirely used in your application then it is best practice to load the following libraries using autoload method. Open application/config/autoload.php. and set your libraries as following:

 /*
| -------------------------------------------------------------------
| Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in system/libraries/ or your
| application/libraries/ directory, with the addition of the
| 'database' library, which is somewhat of a special case.
|
| Prototype:
|
| $autoload['libraries'] = array('database', 'email', 'session');
|
| You can also supply an alternative library name to be assigned
| in the controller:
|
| $autoload['libraries'] = array('user_agent' => 'ua');
*/

$autoload['libraries'] = array('database', 'form_validation', 'session','parser');

 

<<Previous                                                                              Next>>

The following two tabs change content below.

Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

Leave a Reply

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

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