CodeIgniter - Libraries

CodeIgniter - Libraries

CodeIgniter - Libraries

An important part of the CodeIgniter platform are its libraries. It provides a rich set of libraries that indirectly increase the speed of application development. The system library is located in system/libraries. All we have to do is download the library 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 we want to load. If we want to load multiple libraries, then we can simply pass an array as an argument to the library() function as shown below −

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

Library classes

The library classes are located in system/library . Each class has different functions to simplify development work. The following table lists the library class names and descriptions.

Following are the most commonly used library classes.

SN

Library class and description

one

Benchmarking class

The benchmarking class is always active, allowing you to calculate the time difference between any two marked points.

2

Caching class

This class will cache pages to quickly access page speed.

 

3

Calendar class

Using this class, you can dynamically create calendars.

4

Shopping cart class

Using this class, you can add or remove an item from the cart. Items are saved in the session and remain active while the user is not browsing the site.

5

config class

With this class, you can get configuration settings. This class is initialized automatically.

6

Email class

This class provides functionality related to email, such as sending or replying to email.

7

Encryption class

This class provides two-way data encryption.

eight

File upload class

This class provides functionality related to file uploads. You can set various options such as upload file type, file size, etc.

9

Form validation class

This class provides various functions for form validation.

10

FTP class

This class provides various FTP related functions such as transferring files to delete a server, moving, renaming or deleting files on a server.

eleven

Image manipulation class

With this class, you can perform image manipulations such as resizing, thumbnailing, cropping, rotating, watermarking.

12

Input class

This class preprocesses input for security reasons.

thirteen

Language class

This class is used for internationalization.

14

Loader class

This class loads items like File Viewer, Drivers, Helpers, Models, etc.

15

Migrations class

This class provides functionality related to database migration.

sixteen

output class

This class sends output to the browser and also caches this web page.

17

Pagination class

This class adds pagination functionality to a web page.

eighteen

Parser Class Template

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

nineteen

Safety class

This class contains security features like XSS Filtering, CSRF, etc.

twenty

Session Library

This class provides the functionality to maintain your application's session.

21

HTML table

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

22

Trackback class

The Trackback class provides functions that allow you to send and receive Trackback data.

23

Typography Class

The typography class provides methods that help format text.

24

Unit test class

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

25

URI class

The URI class provides methods that help you get information from your URI strings. If you are using URI routing, you can also get information about redirected 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 information about the referrer, as well as information about the language and supported character sets.

27

XML-RPC and XML-RPC Server Classes

The CodeIgniter XML-RPC classes allow you to send requests to another server or configure your own XML-RPC server to accept requests.

28

Zip encoding class

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

Benchmarking class

The benchmarking class is always active, allowing you to calculate the time difference between any two marked points.

Caching class

This class will cache pages to quickly access page speed.

Calendar class

Using this class, you can dynamically create calendars.

Shopping cart class

Using this class, you can add or remove an item from the cart. Items are saved in the session and remain active while the user is not browsing the site.

config class

With this class, you can get configuration settings. This class is initialized automatically.

Email class

This class provides functionality related to email, such as sending or replying to email.

Encryption class

This class provides two-way data encryption.

File upload class

This class provides functionality related to file uploads. You can set various options such as upload file type, file size, etc.

Form validation class

This class provides various functions for form validation.

FTP class

This class provides various FTP related functions such as transferring files to delete a server, moving, renaming or deleting files on a server.

Image manipulation class

With this class, you can perform image manipulations such as resizing, thumbnailing, cropping, rotating, watermarking.

Input class

This class preprocesses input for security reasons.

Language class

This class is used for internationalization.

Loader class

This class loads items like File Viewer, Drivers, Helpers, Models, etc.

Migrations class

This class provides functionality related to database migration.

output class

This class sends output to the browser and also caches this web page.

Pagination class

This class adds pagination functionality to a web page.

Parser Class Template

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

Safety class

This class contains security features like XSS Filtering, CSRF, etc.

Session Library

This class provides the functionality to maintain your application's session.

HTML table

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

Trackback class

The Trackback class provides functions that allow you to send and receive Trackback data.

Typography Class

The typography class provides methods that help format text.

Unit test class

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

URI class

The URI class provides methods that help you get information from your URI strings. If you are using URI routing, you can also get information about redirected segments.

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 information about the referrer, as well as information about the language and supported character sets.

XML-RPC and XML-RPC Server Classes

The CodeIgniter XML-RPC classes allow you to send requests to another server or configure your own XML-RPC server to accept requests.

Zip encoding class

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

Creating Libraries

CodeIgniter has a rich set of libraries that you can find in the system/libraries folder, but CodeIgniter is not only limited to system libraries, you can also create your own libraries which can be stored in the application/library folder . You can create libraries in three ways.

  • Create a new library
  • Extend native library
  • Replace native library

Create a new library

When creating a new library, consider the following:

  • The file name must start with a capital letter, for example, Mylibrary.php
  • The class name must start with a capital letter, for example, the class Mylibrary
  • The class name and file name 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 a User 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 both lowercase and uppercase letters. Use the library name without the ".php" extension. After loading the library, you can also call the function of this class as shown below.

$this->mylibrary->some_function();

Extend native library

Sometimes you may need to add your own functions to the library provided by CodeIgniter. CodeIgniter provides a means by which you can extend your own library and add your own functionality. In order to achieve this, you must extend the native library class. For example, if you want to extend the email library, this can be done as shown below:

Class MY_Email extends CI_Email {

}

Here, in the example above, the MY_Email class extends the CI_Email native library email class. This library can be loaded using the standard email library loading method. Save the above code in My_Email.php file

Replace native library

In some situations you don't want to use your own 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 in the native library. For example, if you want to replace the Email class , use the code as shown below. Save your filename with Email.php and give the class name CI_Email .

email.php

Class CI_Email {

}