CodeIgniter Auxiliary functions

Source: Internet
Author: User

Auxiliary functions are functions that help us accomplish a particular task. Each auxiliary function file is simply a collection of functions. For example, URL Helpers can help us create a link, form Helpers can help us create a form, Text Helpers provides a series of formatted output, cookie Helpers can help us set up and read cookies, File Helpers can help us with documents, and so on. Unlike other parts, auxiliary functions are not implemented in the same way as classes. They are just simple process-handling 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 an auxiliary function, you must first load it. Once loaded, the auxiliary function is globally available. Auxiliary function files are generally saved in the System/helpers or Application/helpers folder. CodeIgniter will first look for the corresponding auxiliary function file in Application/helpers, if the directory does not exist or the directory does not have a corresponding auxiliary function file, CI will be loaded into the auxiliary function file under System/helpers.

Loading the auxiliary functions is very simple:

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

Name is the name of the auxiliary function file (without the. php suffix and the "helper" section).
For example, to load a URL helper with a file name of url_helper.php, you would use the following statement:

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

Once you have loaded the file you want to use for the helper function, you can use the function inside it using the standard function call method.
For example, to use the anchor () function to create a link, you can do this in a view file:

Echo anchor (' blog/comments ', ' Click here ');? >

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

If you want to "extend" an existing helpers, you can create a new helper in your application/helpers/directory, the name of the new helper is added to the "extended" helper's name at the beginning of a my_, where the prefix is configurable , to set your own custom prefix, open the application/config/config.php file and locate the following entry:

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

If all you want to do is add some new features to the original helper, such as adding one or two new methods, or modifying 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 approach is procedural (procedural) and discrete (discrete), which cannot be "extended" in traditional locales, but in codeigniter you can add or modify helper methods.
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 functionfunctionAny_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 functionfunctionRandom_element ($array){    Shuffle($array); return Array_pop($array);}

CodeIgniter Auxiliary functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.