CodeIgniter Auxiliary function Helper detailed _ios

Source: Internet
Author: User
Tags autoload comments extend php and php syntax error blank page codeigniter

1. Overview of accessibility functions

Helper auxiliary functions, as the name suggests, are a series of functions that help us accomplish a variety of specific tasks. and each auxiliary function file is a collection of features that are aggregated together. For example, you can help us create a link URL Helpers, there is a form to create a table Helpers, text formatted with the output Helpers, there is a cookie to set up and read Cookies Helpers, as well as files file Helpers and so on.

Unlike most other systems, CodeIgniter's helper functions are not implemented in a class fashion. But simply, the function of the program. Each auxiliary function handles a specific task and does not have to rely on other functions.

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

Auxiliary function files are generally saved in the System/helpers or Application/helpers folder. CodeIgniter will first find the corresponding auxiliary function file in Application/helpers, if the directory does not exist or the directory does not have corresponding auxiliary function files, CI will be loaded into the system/helpers under the auxiliary function file.

2. Loading auxiliary functions

Loading the auxiliary function is very simple, as follows:

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

Where name is the name of the secondary 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 can use the following statement:

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

Auxiliary functions can be loaded anywhere in your controller (Controller) and can even be loaded in the view file (we do not recommend this). Please load them before using the auxiliary function. You can load them in your controller constructor so that the helper functions can be loaded automatically before other functions. You can also load it on the spot where you want to use the accessibility function.

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

3. Load multiple Auxiliary functions

If you want to load multiple auxiliary functions at once, you can do this:

$this->load->helper (Array (' Helper1 ', ' helper2 ', ' Helper3 '));

4. Automatically load auxiliary functions

CodeIgniter can automatically load auxiliary functions for you if you want. You can do this by opening the application/config/autoload.php and adding an auxiliary function to the automatically loaded array (autoload array).

5. Using Auxiliary functions

Once you've loaded in to use the auxiliary function file, you can use the standard function call method to work with the inside function.

For example, to use the anchor () function to create a link, you can do so in the view file:

<?php echo anchor (' Blog/comments ', ' Click here ');? >

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

Note: the function name in the auxiliary function is best to name the specification, and if multiple auxiliary function files are loaded at the same time, and there are functions with the same name, there will be a blank page problem with CI (this is also a PHP syntax error).

6. "Extended" auxiliary functions

If you want to "expand" an existing Helpers, you can create a new helper in your application/helpers/directory, and the name of the new helper is to add a my_ to the name of the "Extended" helper (this is configurable.) See below .).

If all you want to do is add 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 term "extension" 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 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 rewrite (override) Some of these methods:

Any_in_array () is isn't 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 FALSE;
Random_element () is included into Array Helper, so it overrides the native function
function random_element ($array) c15/>{
  Shuffle ($array);
  Return Array_pop ($array);

7. Set your own prefix (Prefix)

Files that are prefixed to the "extend" helper are also extensions to libraries and core classes. To set your custom prefix, open the application/config/config.php file, and then locate the following entry:

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

Note here: Since all CodeIgniter libraries are named with prefixes such as ci_, do not use ci_ to customize prefixes.

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: 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.