Auxiliary functions are functions that help us complete specific tasks. Each helper function file is just a collection of functions. For example, URL Helpers can help us create links, Form Helpers can help us create forms, Text Helpers provide a series of formatted output methods, Cookie Helpers can help us set and read COOKIE, File Helpers can help us process files, etc. . Unlike other parts, auxiliary functions are not implemented in classes. They are just simple procedural processing functions. Each helper function handles a specific task and does not have to rely on other functions.
CodeIgniter does not load the auxiliary function file by default, so if you want to use the auxiliary function, you must load it first. Once loaded, the helper function will be globally available. Helper function files are generally saved in the system/helpers
or application/helpers folder. CodeIgniter will first search 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.
Loading the helper function is very simple:
[code]$this->load->helper('name');
name is the name of the helper function file (without the .php suffix and the "helper" part).
For example, to load the URL Helper with the filename url_helper.php
, you would use the following statement:
[code]$this->load->helper('url');
Once you load If you want to use the auxiliary function file, you can use the standard function calling method to use the functions inside.
For example, to use the anchor() function to create a link, you can do this in the View file:
[code]<?php echo anchor('blog/comments', 'Click Here');?>
"Click Here" is the link The name, "blog/comments" is the URI of the link.
If you want to "extend" an original Helper, you can create a new helper in your application/helpers/ directory. The name of the new helper is added at the beginning of the name of the "extended" Helper. A MY_, the prefix here is configurable. In order to set your custom prefix, please open the application/config/config.php
file, and then find the following entry:
[code]$config['subclass_prefix'] = 'MY_';
If all you want to do is add some new functions to the original helper, for example, add one or two new methods, or modify a method; it is not worth rewriting your own helper. In this case, it's better to "extend" an existing helper. The word "extended" is not very appropriate here, because Helper's methods are procedural and discrete and cannot be "extended" in traditional language environments. However, in CodeIgniter, you can add Or modify the helper method.
For example, to extend a local existing Array Helper you should create a file: application/helpers/MY_array_helper.php
, and add or override some of its methods:
[code]// 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); }
The above is the content of CodeIgniter study notes Item8--CI auxiliary function. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!