Codeigniter, PHP, Web

Codeigniter create helper

Base64 | Codeigniter | Framework | Helpers | PHP

Codeigniter Create Helpers


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

Codeigniter Create Helper: Codeigniter comes with 20+ built in helpers. Which has tons of functionalities that we can use in our website development. Still there may be time when our requirements do not match with any of available functions. So we have to create our own helpers. Codeigniter Helpers are just collection of related functions wrapped inside a file. So that whenever we need it we can load only that particular helper.

CodeIgniter Create Helper from scratch

Codeigniter create helper is as easy as creating a simple php function. Just have to follow few steps for codeigniter framework. For demo purpose we are creating a function to encode image using base64 method. If you don’t know what is base64 image encoding please refer to Base64 Images Advantages & Disadvantages. Lot’s of request for Base64 encode image for php comes from our visitors. So I thought may be I can integrate it with codeigniter.

Step 1. Create a new file called myimage_helper.php inside application/helpers directory. Codeigniter follows a pattern for helper files. Which is <helper_name>_helper.php.

Step 2. Write following code inside myimage_helper.php file that we just created.

First we check for convertToBase64 function already exists or not. If not then we’ll create a new one. ConvertToBase64 accepts 1 parameter which is $path (location of image file). Next step is to get information of file specified in parameter by using pathinfo function. file_get_contents function will retrieve data from specified file and stores in $data variable. Now the main conversion part comes where we encode image using base64 method. Base64_encode function is already in php to perform that part. Final step is to return base64 converted string in correct format.

Step 3. Load newly created helper in controller __construct or in a method where needed.

 Step 4. Now add view code in views/home.php file or wherever you want to output base64 image.

As you can see, we used new helper method.

Step 5. Now navigate to home controller’s index method. For me it’s http://localhost:8888/CI/index.php/home. You will get your image in browser but i will be in base64 encoded just like below.

Codeigniter create Helper

As you can see, image content is encoded & integrated with HTML code. So no more separate request need for image loading. For Advantages & Disadvantages of doing so please refer to Base64 Images Advantages & Disadvantages.

Our new helper is working and it also outputs image. we can add as many function we want to inside single helper but it is recommended you categorise those functions via multiple helpers.

We finished Codeigniter Create helper Tutorial. If you find it useful for Codeigniter create helper tutorial, please hit like & leave a comment for us. If you have any issue please feel free to mention same in comment so we can serve you better. Visit & like our Facebook Fan Page to get instant update of our website.

Series Navigation<< Codeigniter: How to Pass Data to ViewCodeIgniter Helpers >>

Bookmark Link using: bookmark at folkd