Code igniter helper function helper usage

Source: Internet
Author: User
Code igniter helper function helper usage

  1. $ This-> load-> helper ('name ');

Here, name is the name of the helper function File (without the. php suffix and "helper ).

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

  1. $ This-> load-> helper ('URL ');

Helper functions can be loaded anywhere in your controller, or even in View files (we do not recommend that you do this ). Load them before using helper functions. You can load them in your controller constructor so that helper functions can be automatically loaded before other functions. You can also load it on the spot where helper functions are to be used.

Note: the auxiliary function does not return a value when loading the function, so do not try to pay it to a variable, just use it like this.

3. load multiple auxiliary functions. if you want to load multiple auxiliary functions at a time, you can do this:

  1. $ This-> load-> helper (array ('helper1', 'helper2', 'helper3 '));

4. automatically load the helper function. if you want it, CodeIgniter can automatically load the helper function for you. You can enable application/config/autoload. php and add helper functions to the autoload array.

5. Once a helper function is loaded and you want to use the helper function file, you can use the standard function call method to use the functions in it. For example, to use the anchor () function to create a link, you can do this in the View file:

  1. Echo anchor ('blog/comments', 'Click Here ');
  2. ?>

Click Here is the name of the link, and blog/comments is the URI of the link. (Programmer's house bbs.it-home.org collection) Note: the function name in the helper function is best to do a good job of naming, if the simultaneous loading of multiple helper function files, and which has the same name of the function, it will cause a blank page of CI (which is also a PHP syntax error ).

6. if you want to extend an existing Helpers, you can create a new helper in the application/helpers/directory, the new helper name is added with MY _ at the beginning of the extended Helper name (this can be configured. see .). if you only add some new functions to the original helper, such as adding one or two new methods or modifying a method, it is not worth rewriting your helper. In this case, it is best to "extend" the existing helper. The term "extension" is not very appropriate here, because the Helper method is procedural (procedural) and discrete (discrete, in the traditional language environment, it cannot be "extended", but in CodeIgniter, you can add or modify the helper method. For example, to expand an existing local Array Helper, you should create a file: application/helpers/MY_array_helper.php, and add or override some of the 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 already Ded in Array Helper, so it overrides the native function
  15. Function random_element ($ array)
  16. {
  17. Shuffle ($ array );
  18. Return array_pop ($ array );
  19. }

7. Prefix is used for "extension" helper. files with a Prefix are also extended to the library and core classes. To set the custom prefix, open the application/config. php file and find:

  1. $ Config ['subclass _ prefix'] = 'My _';

Note: because all the libraries that come with CodeIgniter are named with a prefix like 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.