Helper Functions

Helpers, as the name suggests, help you with tasks. Each helper file issimply a collection of functions in a particular category. There are URLHelpers, that assist in creating links, there are Form Helpers that helpyou create form elements, Text Helpers perform various text formattingroutines, Cookie Helpers set and read cookies, File Helpers help youdeal with files, etc.

Unlike most other systems in CodeIgniter, Helpers are not written in anObject Oriented format. They are simple, procedural functions. Eachhelper function performs one specific task, with no dependence on otherfunctions.

CodeIgniter does not load Helper Files by default, so the first step inusing a Helper is to load it. Once loaded, it becomes globally availablein your controller andviews.

Helpers are typically stored in your system/Helpers, orapp/Helpers directory. CodeIgniter will look first in yourapp/Helpers directory. If the directory does not exist or thespecified helper is not located there CI will instead look in yourglobal system/Helpers/ directory.

Loading a Helper

Loading a helper file is quite simple using the following method:

  1. helper('name');

Where name is the file name of the helper, without the .php fileextension or the “helper” part.

For example, to load the Cookie Helper file, which is namedcookie_helper.php, you would do this:

  1. helper('cookie');

If you need to load more than one helper at a time, you can passan array of file names in and all of them will be loaded:

  1. helper(['cookie', 'date']);

A helper can be loaded anywhere within your controller methods (oreven within your View files, although that’s not a good practice), aslong as you load it before you use it. You can load your helpers in yourcontroller constructor so that they become available automatically inany function, or you can load a helper in a specific function that needsit.

Note

The Helper loading method above does not return a value, sodon’t try to assign it to a variable. Just use it as shown.

Note

The URL helper is always loaded so you do not need to load it yourself.

Loading from Non-standard Locations

Helpers can be loaded from directories outside of app/Helpers andsystem/Helpers, as long as that path can be found through a namespace thathas been set up within the PSR-4 section of the Autoloader config file.You would prefix the name of the Helper with the namespace that it can be locatedin. Within that namespaced directory, the loader expects it to live within asub-directory named Helpers. An example will help understand this.

For this example, assume that we have grouped together all of our Blog-relatedcode into its own namespace, Example\Blog. The files exist on our server at/Modules/Blog/. So, we would put our Helper files for the blog module in/Modules/Blog/Helpers/. A blog_helper file would be at/Modules/Blog/Helpers/blog_helper.php. Within our controller we coulduse the following command to load the helper for us:

  1. helper('Modules\Blog\blog');

Note

The functions within files loaded this way are not truly namespaced.The namespace is simply used as a convenient way to locate the files.

Using a Helper

Once you’ve loaded the Helper File containing the function you intend touse, you’ll call it the way you would a standard PHP function.

For example, to create a link using the anchor() function in one ofyour view files you would do this:

  1. <?php echo anchor('blog/comments', 'Click Here');?>

Where “Click Here” is the name of the link, and “blog/comments” is theURI to the controller/method you wish to link to.

“Extending” Helpers

To “extend” Helpers, create a file in your app/Helpers/ folderwith an identical name to the existing Helper.

If all you need to do is add some functionality to an existing helper -perhaps add a function or two, or change how a particular helperfunction operates - then it’s overkill to replace the entire helper withyour version. In this case, it’s better to simply “extend” the Helper.

Note

The term “extend” is used loosely since Helper functions areprocedural and discrete and cannot be extended in the traditionalprogrammatic sense. Under the hood, this gives you the ability toadd to, or to replace the functions a Helper provides.

For example, to extend the native Array Helper you’ll create a filenamed app/Helpers/array_helper.php, and add or overridefunctions:

  1. // any_in_array() is not in the Array Helper, so it defines a new function
  2. function any_in_array($needle, $haystack)
  3. {
  4. $needle = is_array($needle) ? $needle : [$needle];
  5.  
  6. foreach ($needle as $item)
  7. {
  8. if (in_array($item, $haystack))
  9. {
  10. return TRUE;
  11. }
  12. }
  13.  
  14. return FALSE;
  15. }
  16.  
  17. // random_element() is included in Array Helper, so it overrides the native function
  18. function random_element($array)
  19. {
  20. shuffle($array);
  21. return array_pop($array);
  22. }

The helper() method will scan through all PSR-4 namespaces defined in app/Config/Autoload.phpand load in ALL matching helpers of the same name. This allows any module’s helpersto be loaded, as well as any helpers you’ve created specifically for this application. The load orderis as follows:

  • app/Helpers - Files loaded here are always loaded first.
  • {namespace}/Helpers - All namespaces are looped through in the order they are defined.
  • system/Helpers - The base file is loaded last

Now What?

In the Table of Contents, you’ll find a list of all the available HelperFiles. Browse each one to see what they do.