CodeIgniter Auxiliary Function Helper usage detailed

Source: Internet
Author: User
Tags autoload php syntax error blank page codeigniter
    1. $this->load->helper (' name ');
Copy Code

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

    1. $this->load->helper (' url ');
Copy Code

The helper functions can be loaded anywhere in your controller (controllers) and can even be loaded in a view file (which we do not recommend). Please load them before using the helper functions. You can load them in your controller constructors 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 helper function.

Note: The auxiliary function load function does not return a value, so do not attempt to pay it to a variable, just as you would use it.

3, loading multiple auxiliary functions if you want to load multiple auxiliary functions at once, you can do this:

    1. $this->load->helper (Array (' Helper1 ', ' helper2 ', ' Helper3 '));
Copy Code

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

5, using an auxiliary function once you have loaded the desired auxiliary function file, 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:

    1. echo anchor (' Blog/comments ', ' Click here ');
    2. ?>
Copy Code

Here the "Click here" is the name of the link, "Blog/comments" is the URI of the link. (Programmer's House bbs.it-home.org Collection) Note: The function name in the auxiliary function is best to name the specification, if you load multiple auxiliary function files at the same time, and there are functions with the same name, it will cause a blank page problem for CI (this is also a PHP syntax error).

6, "extended" auxiliary function if you want to "extend" an existing Helpers, you can create a new helper in the application/helpers/directory, the name of the new helper is to add a my_ to the name of the "Extended" helper ( This can be configured. See below.). If you just add some new functionality 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:

    1. Any_in_array () is not in the array Helper, so it defines a new function
    2. function Any_in_array ($needle, $haystack)
    3. {
    4. $needle = (Is_array ($needle))? $needle: Array ($needle);
    5. foreach ($needle as $item)
    6. {
    7. if (In_array ($item, $haystack))
    8. {
    9. return TRUE;
    10. }
    11. }
    12. return FALSE;
    13. }
    14. Random_element () is included in Array Helper, so it overrides the native function
    15. function Random_element ($array)
    16. {
    17. Shuffle ($array);
    18. Return Array_pop ($array);
    19. }
Copy Code

7, the set prefix (Prefix) for "extended" helper and prefix file is also an extension of the library and core classes. In order to set a custom prefix, open the application/config/config.php file and locate:

    1. $config [' subclass_prefix '] = ' my_ ';
Copy Code

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

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