Codeigniter, PHP

Codeigniter: Creating Custom Library in Codeigniter

Codeigniter | Codeigniter Library | Framework | PHP

Creating Custom Library in Codeigniter


This entry is part 7 of 8 in the series Learn Codeigniter

Creating Custom Library in Codeigniter is not that tough but first understand why we need one. Codeigniter comes with many built-in libraries which are very useful in common website development. But often times we find that if i have library for this functionality then i don’t have to repeat code over and over again. Now helpers can do same task. Libraries can arrange similar functionalities in a single file. Unlike helpers, library has its own object to store related data.

Creating Custom Library in Codeigniter

In our previous part How to use Codeigniter email Library of course Learn Codeigniter, We learn how to use codeigniter’s built-in library. Now in this part we will create custom library from scratch. For simplicity, we will use simple concept like creating our own email notification library.

Download Source Code Files

Step 1: For Creating Custom Library in Codeigniter, Create a new file inside application/libraries directory and name it notify.php. write following code inside notify.php file.

First line is for security reason. It makes sure that direct access to this file is not allowed. Next we declare a class with name exactly similar to what we name our library file. Remember, Naming convention is very important in codeigniter. We also want to create a constructor for Notify class. Incase if we need to use codeigniter’s other library for certain tasks like retrieve data from database or insert data in database. We can do that by fetching Codeigniter global instance via get_instance method and store it in instance variable of class ($this->ci).

Step 2: Declare send mail function who is responsible for sending mail. Now we are using codeigniter’s built-in email library for sending mail. add highlighted code in Notify class which declare a function named send_mail.

send_mail function accepts 5 parameters. 4 Parameters are optional which first one is required. As you can see we hard coded smtp settings but if we want to make it dynamic, we can do that inside library by storing settings in instance variable. Just like we did for CI instance. We pass config variable in load library method. Next we have to set To, From, bcc, subject & message. Remember, we are using $this->ci->email instead of $this->email in every line. finally we have to send email by calling send method of email library. Last line is to debug email, you have to comment it once tested. By creating this function now we dont have to write long code for sending mail every time.

Step 3: Declare another method in Notify library to add functionality of sending contact us email.

This function will accept 1 parameter which is contact array containing name, email, subject & message. We build a format and store it in message variable. Next step is to pass data (to, subject & message) to send_mail function. Which we created in previous step. Remember from and bcc parameters are optional. So everything so far is nice and clear. Let’s move on to next step for Creating Custom Library in Codeigniter and use our library in controller.

Step 4: Create controller or use existing controller. For this example we are using controller that we created in our previous tutorial. Create a function in controller and also create a view for that inside views directory.

Code for Controller Method:

Code for Contact View:

We created a form with action points to sendContactMail method of Home Controller. And 4 inputs for name, email, subject & message.

Note: We have used site_url function which is located inside url helper. So make sure you have loaded in config/autoload.php file or explicitly via load helper method.

At this moment if we navigate to home/contact page we will get output like below:

Creating Custom Library in Codeigniter Contact-Form

I know It’s not that fancy but CSS is not what we are learning here. Let’s move on to next step for Creating Custom Library in Codeigniter. We use submitted data and send email using our newly created custom codeigniter library.

Step 5: Create a new function in home controller named sendContactMail with following code.

Because I dont have any other data in POST array. I used whole $_POST array for contact details. We load library via load library method. It will create an instance variable with same name in $this (CI Instance) object. next we call contact_mail method by passing contact variable and that’s it.

Download Source Code Files

For more updates on Facebook please like our Facebook Fan Page.

Series Navigation<< How to send email in Codeigniter email Library

Bookmark Link using: bookmark at folkd