×

CodeIgniter Creating Library

As we know, CodeIgniter has a large collection of library functions that are stored in the system/libraries folder. CodeIgniter also provides some additional functionality in library files. For example, if you want to create custom library files, it allows you to create and save it in the application/libraries folder.

CodeIgniter provides three ways to create library files in the application,

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

Create a new library

You must remember that when you are creating a new library file, it should be placed in the application/library folder. And you also have to remember these things while creating libraries files such as:

  • The first letter of the library file name must be in uppercase e.g., Mainlibrary.php
  • The class name of a library file must be in uppercase letter class e.g., class Mainlibrary
  • The name of the class and library file must match.

Mainlibrary.php

<?php
 defined('BASEPATH') OR exit('No direct script access allowed');
 class Mainlibrary {
         public function show_function()
         {
         }
 } 

Loading a library file in the controller

After creating a mainlibrary.php file, you should load it into the controller file by following the syntax:

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

In the above Syntax, ‘Mainlibrary’ is the name of library file. The name of the library file can be in uppercase as well as a lowercase letter. You have to load a library file without a ‘.php’ extension in the controller. Once the library file is loaded in the controller, then you can call any methods of that class by following methods as shown below.

$this->Mainlibrary->show_function();

Extend a native library

If you want to add functionality to an existing library, CodeIgniter provides a native library. In this library, you can extend a native library class and add one or more methods to it. To achieve the same, you should extend an existing library class. The following points must be remembering:

  • The declaration of a new library class should extend the predefined base class in CodeIgniter.
  • MY_ should be used as a prefix in the library's class and file name.

Example: This is the method to extend a native session library class. You have to create a MY_Session native library class and save it in the application/libraries/MY_Session.php,

MY_Session.php

class MY_Session extends CI_Session {
             public function _construct ( $config = array() )
             {
                         parent:: _contsruct ( $config );
             }
             public function display()
 {
             }
 } 

Replace a native library

CodeIgniter also provides some flexibility, such as if you don't want to use the native library classes, you can replace it with your new library files. To achieve the functionality, you must declare the same name as the library class and file that is the same as the original library class in CodeIgniter. For example, if you want to change a native cache library, you have to create a class name ‘CI_Cache’ and save the ‘Cache.php’ file to application/library/Cache.php.

Cache.php

class  CI_Cache {
 } 


Related Topics

CodeIgniter Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 minutes read.

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

11 minutes read.

Download Helper Codeigniter

Download Helper Codeigniter: The download helper function is used to download data from the server to your computer. The data can be any format like text, jpg, mp3, mp4, etc. Loading...

2 minutes read.

CodeIgniter Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

10 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

7 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

CodeIgniter Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

8 minutes read.

CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc. Loading an Inflector Helper Before using an inflector helper function, you must...

3 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

CodeIgniter Cookie Helper

A cookie is a small set of files sent from the web server to the end-user system. In Helpers, the cookie helper file has some predefined functions that are used to the...

4 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 minutes read.