Chapter 5. Helpers

This chapter covers the CI helpers topic, the different types of helpers, and their different usage categories, with several code examples of web applications. CI provides us with built-in helpers, enables us to integrate third-party helpers, and enables us to develop new helpers and share them with the community if we wish to. The CI helpers are powering CI efficiency and code reusability by enabling all other CI controllers using the same code instead of defining a helper function locally.

A helper file is a collection of independent procedural functions in a particular category. Each helper function performs one specific task, with no dependence on other functions. The chapter will elaborate on the CI helper's concept, definition, and usage with several examples.

The folder system/helpers contains the CI system's built-in helpers. The folder application/helpers contains all the additional helper files of CI helpers. They can be third-party helpers or created by the developer.

This chapter will primarily focus on:

  • CI helpers' scope and usage
    • Usage categories
    • Using a helper
    • Adding a helper to the project
    • Loading a helper
    • Using helper methods
  • The available CI helpers
  • Examples
    • Example 1: using a built-in helper
    • Example 2: using third-party helpers—SSL helper
    • Example 3: building our own helper—the my_download helper

We will begin by briefly reviewing what a helper is in a CI framework, and how we can use it for our needs across the project code resources.

CI helpers' scope and usage

The CI helper does not have access to the controller resources by default unless CI and get_instance() are called and used to access CI resources.

We can extend the CI helper using third-party helpers from the CI system' or we can develop our own helper.

Any application helper should be located under application/helpers/ in the project directory.

The helper file must be in the following format:

  application/helpers/<HELPER_NAME>_helper.php

For example, the SSL helper file should appear as application/helpers/ssl_helper.php.

The helper integration and usage within the CI project is as follows:

  • Add the helper code resources to application/helpers/myhelper_helper.php
  • Load the helper automatically or via the controller
    • Automatically load a helper myhelper for all CI projects as follows:
      $autoload['helper'] = array('url','myhelper'),
      
    • For loading in certain specific controllers, constructors, or methods, use the following:
      $this->load->helper('myhelper'), 
      
  • Use the following helper methods:
    $result = $this->myhelper->called_method($param1, aram2);
    

Available CI helpers

CI and the CI developers community network provide many helpers, covering a rich set of topics. We will review CI helpers as well as popular resources for third-party CI helpers.

We are also encouraged to build our own helpers, which can be used by others, and share them with the following communities:

CI system helpers

The list of CI built-in helpers is as follows (they can be found in the CI Directory Tree by going to system/helpers/):

  • Array Helper
  • CAPTCHA Helper
  • Cookie Helper
  • Date Helper
  • Directory Helper
  • Download Helper
  • Email Helper
  • File Helper
  • Form Helper
  • HTML Helper
  • Inflector Helper
  • Language Helper
  • Number Helper
  • Path Helper
  • Security Helper
  • Smiley Helper
  • String Helper
  • Text Helper
  • Typography Helper
  • URL Helper
  • XML Helper

CI third-party helpers

  • ssl_helper.php
  • html_manipulator_helper.php
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.151.45