The helpers in CodeIgniter to define our functions

- Andrés Cruz

En español

The helpers in CodeIgniter to define our functions

The helpers in CodeIgniter are files that store a set of globally defined functions throughout our application based on the same classification or on the same type.

Helpers are one of the two ways that exist to extend specific functionality on the platform; the other is the bookstores; another definition they may have is a library of functions that resolve typical items and in a particular category.

In short, helpers are a simple way for us to create functionality in CodeIgniter; they are a set of related functions that are stored in a single file.

The Helpers in CodeIgniter

CodeIgniter has multiple Helpers that we can see in the official documentation.

CodeIgniter does not load helpers by default, all the helpers that we want to use must be loaded either by autoload or by the traditional way.

The system helpers are stored in system/helpers while the helpers that we are going to define are stored in system/application/helpers

Working with the helpers in CodeIgniter

The helpers in CodeIgniter are stored in the system/helpers folder; as you will see, they have different purposes, to work with URLs, texts, dates, captchas, etc.

Load helper in CodeIgniter

To load a helper like in CodeIgniter we can do it individually as follows:

$this->load->helper('helper');

Or we can load several of them as follows:

$this->load->helper(array('helper1', 'helper2', 'helper3'));

We can also perform the upload in an automated way so that it is available in the whole scope of our application in the following way:

application/config/autoload.php

For example:

$autoload['helper'] = array('html', 'url', 'form', 'utils_helper');

In general, we can carry out the loads in a very similar way to what CodeIgniter allows us in the rest of the elements, such as libraries or models.

Crear nuestros propios helpers

This is one of the components in CodeIgniter that we can create ourselves; that is, we can have our custom helpers; For example, let's create a helper called utils_helper in the following directory:

application\helpers\

That is to say:

application\helpers\utils_helper.php

And let's define the following function:

if (!function_exists('format_dates')) {
    function format_dates($date) {
        $CI = & get_instance();
        $date = new DateTime($date);
        return $date->format('d/m h:m');
    }
}

We can also define it as follows:

    function format_dates($date) {
        $CI = & get_instance();
        $date = new DateTime($date);
        return $date->format('d/m h:m');
}

In general, what this function does is given a date, it returns the date in another format. We can also define the functions that we need, although remember that they must be functions of the same class; for example:

    function format_dates($date) {
        $CI = & get_instance();
        $date = new DateTime($date);
        return $date->format('d/m h:m');
}
    function format_dates_other_contry($date) {
    // hacer algo
}

And just like that, we define two functions for the same helper; To use any of the previous functions, we must first load the helper

$this->load->helper('name');

And then we just call the method wherever we want:

format_dates($date);

Extend helpers in CodeIgniter

We may also add new functions or features on top of existing helpers or our own; although the common thing is to extend functions on the existing ones; that is, about the helpers that are stored in system/helpers; for this we must create our helpers in the system/application/helpers folder and name it as the original helper but with the prefix "MY_"; simple as that

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 1d 07:50!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!