Details of CodeIgniter auxiliary function helper

  • 2020-05-06 11:41:59
  • OfStack

1. Overview of auxiliary functions

helper auxiliary functions, as the name implies, are a series of functions that help us accomplish various specific tasks. And each helper function file is a collection of functions put together. For example, URL Helpers helps us create links, Form Helpers helps us create tables, Text Helpers helps us format text output, Cookie Helpers helps us set and read cookie Cookie Helpers, File Helpers and so on.

Unlike most other systems, CodeIgniter's helper functions are not implemented as classes. It's a simple, procedural function. Each helper function handles a specific task and does not have to rely on other functions.

CodeIgniter is not loaded by default, so if you want to use an auxiliary function, you must load it first. Once loaded, helper functions are available globally (globally available), which you can use in controller and views.

Auxiliary function files are generally stored in the system/helpers or application/helpers folders. CodeIgniter will first look for the corresponding auxiliary function file in application/helpers. If the directory does not exist or there is no corresponding auxiliary function file in the directory, CI will load the auxiliary function file under system/helpers.

2. Load the helper function

Loading helper functions is very simple, as shown below :


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

Where name is the name of the auxiliary function file (without the.php suffix and the "helper" section).

For example, to load URL Helper with the file name url_helper.php, use the following statement:


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

Helper functions can be loaded anywhere in your controller (controller), even in the view (View) file (we don't recommend this). Please load the helper functions before using them. You can load them in your controller constructor so that helper functions are automatically loaded before other functions. You can also load it on the spot where you want to use the helper function.

Note: the helper load function does not return a value, so don't try to pay it to a variable, just use it like this.

Load multiple helper functions

If you want to load more than one helper function at a time, you can do this :


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

4. Automatically loads the helper function

CodeIgniter can automatically load helper functions for you if you want. You can open the application/config/autoload php, and to automatically load array (autoload array) adding auxiliary function.

5. Use the helper function

Once you have loaded the helper function file that you want to use, you can use the standard function call method to use the functions inside.

For example, to create a link using the anchor() function, you can do this in the view (View) file :


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

Here "Click Here" is the name of the link, and "blog/comments" is the URI of the link.

Note: it is a good idea to name the functions in auxiliary functions. If multiple auxiliary function files are loaded at the same time and there are functions with the same name, CI will have a blank page problem (this is also an PHP syntax error).

6. "extend" the helper function

If you want to "extend" an existing Helpers, you can create a new helper in your application/helpers/ directory

If all you want to do is add some new functionality to helper, for example, add a new method or two, or change a method; It's not worth rewriting your helper. In this case, it is best to "extend" the existing helper. The word "extended" is not appropriate here, because the Helper method is procedural (procedural) and discrete (discrete) and cannot be "extended" in a traditional locale, but in CodeIgniter you can add or modify the helper method.

For example, to extend a locally existing Array Helper you should create a file: application/helpers/ MY_array_helper.php, and add or override (override) some of these methods :


// any_in_array() is not in the Array Helper, so it defines a new function
function any_in_array($needle, $haystack)
{
  $needle = (is_array($needle)) ? $needle : array($needle);
  foreach ($needle as $item)
  {
    if (in_array($item, $haystack))
    {
      return TRUE;
    }
    }
  return FALSE;
}
// random_element() is included in Array Helper, so it overrides the native function
function random_element($array)
{
  shuffle($array);
  return array_pop($array);
}

7. Set your own prefix (Prefix)

For files helper and prefixed with "extension" is also to the library and the expansion of the core classes. In order to set up your custom prefix, please open the application/config/config php file, and then find the following items:


$config['subclass_prefix'] = 'MY_';

Note here: since all CodeIgniter libraries are named with a prefix like CI_, do not use the CI_ custom prefix.


Related articles: