CodeIgniter - Libraries



The essential part of a CodeIgniter framework is its libraries. It provides a rich set of libraries, which indirectly increase the speed of developing an application. The system library is located at system/libraries. All we need to do is to load the library that we want to use. The library can be loaded as shown below −

$this->load->library('class name');

Where class name is the name of the library that we want to load. If we want to load multiple libraries, then we can simply pass an array as argument to library() function as shown below −

$this->load->library(array('email', 'table'));

Library Classes

The library classes are located in system/libraries. Each class has various functions to simplify the developing work. Following table shows the names of the library class and its description.

S.N. Library Class & Description
1

Benchmarking Class

Benchmarking class is always active, enabling the time difference between any two marked points to be calculated.

2

Caching Class

This class will cache the pages, to quickly access the page speed.

3

Calendaring Class

Using this class, you can dynamically create calendars.

4

Shopping Cart Class

Using this class, you can add or remove item from Shopping Cart. The items are saved in session and will remain active until the user is browsing the site.

5

Config Class

Configuration preferences can be retrieved, using this class. This class is initialized automatically.

6

Email Class

This class provides email related functionality, like send or reply to email.

7

Encryption Class

This class provides two-way data encryption functionality.

8

File Uploading Class

This class provides functionalities related to file uploading. You can set various preferences like type of file to be uploaded, size of the files etc.

9

Form Validation Class

This class provides various functions to validate form.

10

FTP Class

This class provides various FTP related functions like transferring files to remove server, moving, renaming or deleting files on server.

11

Image Manipulation Class

Manipulation of image like resize, thumbnail creation, cropping, rotating, watermarking can be done with the help of this class.

12

Input Class

This class pre-processes the input data for security reason.

13

Language Class

This class is used for internationalization.

14

Loader Class

This class loads elements like View files, Drivers, Helpers, Models etc.

15

Migrations Class

This class provides functionalities related to database migrations.

16

Output Class

This class sends the output to browser and also, caches that webpage.

17

Pagination Class

This class adds pagination functionalities to web page.

18

Template Parser Class

The Template Parser Class can perform simple text substitution for pseudo-variables contained within your view files. It can parse simple variables or variable tag pairs.

19

Security Class

This class contains security related functions like XSS Filtering, CSRF etc.

20

Session Library

This class provides functionalities to maintain session of your application.

21

HTML Table

This class is used to auto-generate HTML tables from array or database results.

22

Trackback Class

The Trackback Class provides functions that enable you to send and receive Trackback data.

23

Typography Class

The Typography Class provides methods that help to format text.

24

Unit Testing Class

This class provides functionalities to unit test your application and generate the result.

25

URI Class

The URI Class provides methods that help you retrieve information from your URI strings. If you use URI routing, you can also retrieve information about the rerouted segments.

26

User Agent Class

The User Agent Class provides functions that help identify information about the browser, mobile device, or robot visiting your site. In addition, you can get referrer information as well as language and supported character-set information.

27

XML-RPC and XML-RPC Server Classes

CodeIgniter’s XML-RPC classes permit you to send requests to another server, or set up your own XML-RPC server to receive requests.

28

Zip Encoding Class

This class is used to create zip archives of your data.

Creating Libraries

CodeIgniter has rich set of libraries, which you can find in system/libraries folder but CodeIgniter is not just limited to system libraries, you can create your own libraries too, which can be stored in application/libraries folder. You can create libraries in three ways.

  • Create new library
  • Extend the native library
  • Replace the native library

Create New Library

While creating new library one should keep in mind, the following things −

  • The name of the file must start with a capital letter e.g. Mylibrary.php
  • The class name must start with a capital letter e.g. class Mylibrary
  • The name of the class and name of the file must match.

Mylibrary.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
   
   class Mylibrary {
	
      public function some_function() {
      }
   }
	
/* End of file Mylibrary.php */

Loading the Custom Library

The above library can be loaded by simply executing the following line in your controller.

$this->load->library(‘mylibrary’);

mylibrary is the name of your library and you can write it in lowercase as well as uppercase letters. Use the name of the library without “.php” extension. After loading the library, you can also call the function of that class as shown below.

$this->mylibrary->some_function();

Extend the Native Library

Sometimes, you may need to add your own functionality to the library provided by CodeIgniter. CodeIgniter provides facility by which you can extend the native library and add your own functions. To achieve this, you must extend the class of native library class. For example if you want to extend the Email library then it can be done as shown below −

Class MY_Email extends CI_Email { 
}

Here, in the above example, MY_Email class is extending the native library’s email class CI_Email. This library can be loaded by the standard way of loading email library. Save the above code in file My_Email.php

Replace the Native Library

In some situations, you do not want to use the native library the way it works and want to replace it with your own way. This can be done by replacing the native library. To achieve this, you just need to give the same class name as it is named in native library. For example, if you want to replace the Email class, then use the code as shown below. Save your file name with Email.php and give a class name to CI_Email.

Email.php

Class CI_Email { 
}
Advertisements