CodeIgniter Libraries
×

CodeIgniter Libraries

0 888

  • A CodeIgniter library is a PHP class. The scope of the library can be any project resource, such as helpers, models, controllers, and views.
  • The CodeIgniter libraries are powering efficiency, code reusability, separation, and simplicity.

  • Related topic:

    Codeigniter Interview Questions

  • In CodeIgniter, we have two types of libraries:
  • Built-in libraries: These are stored in the system/libraries directory.
  • Custom libraries: These are created in the application/libraries directory.
  • To reuse the code, the libraries can be loaded by any project controller, model, or helper anywhere in the Codeigniter project.
  • The use of libraries makes the project easy to understand and maintain.

Loading a library:

We can load a library by two methods:

1 Automatically loading: We can load a library automatically by adding a given line in the application/cofig/autoload.php file.

$autoload['libraries'] = array('database','my_lib');

Here, my_lib is the library that we want to load automatically for the entire project.

2 Manually loading: We can add a library in a certain controller, model, or method by the following line.

$this->load->library('my_lib');
Here, my_lib is the name of the library that we want to load.

To load more than one library, we use a multiple array that contains all libraries.

$this->load->library(array(' form_validation ', ' pagination '));

Creating a library:

These libraries are stored in the application/libraries folder. There are three methods to create a custom library.

  • Creating a new library
  • Extending a native library
  • Replacing a native library

1 Creating a new library:

  • To create a new library, open the application/libraries directory.
  • The first letter of the file name must be in uppercase.
  • The first letter of the library class name must be in uppercase.
  • The file name and class name must be the same for better convenience.
  • Syntax:

    $this->load->library('Custom_library.php')

    Here, Custom_library.php is the name of the library file created in the application/libraries directory.

    Note:

    we can also use custom_ library.php instead of Custom_library.php in the above line.

  • To access this library, use the following line.
$this-> custom_ library ->lib_method();

Here, lib_method is a function of library custom_ library.

2 Extending a native library:

  • Codeigniter provides the facility that you can add one or more methods in the old library to extend the functionality of it. There are some rules which should be followed:
  • The New class extends the native library class.
  • Every new class start with the prefix MY_.

For example: To extend the CodeIgniter table library. Open application/ libraries directory. Create a file MY_Table.php and create a class in it which extends the CI_Table class as:

Class MY_ Table extends CI_ Table {
…………
}

3 Replacing a native library: CodeIgniter provides the facility that you can override a native library.

To replace a native library, create a new file with the same name in the application/ libraries directory.

For example: To replace the table library, create a file Table.php in the application/ libraries directory. In this file create class CI_Table.

Class CI_Table {
……………
}


Best WordPress Hosting


Share:


Leave a Reply


Comments
    Waiting for your comments