- $ 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:
- $ 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:
- $ 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:
- Echo anchor ('blog/comments', 'Click Here ');
- ?>
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:
- // 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 already Ded in Array Helper, so it overrides the native function
- Function random_element ($ array)
- {
- Shuffle ($ array );
- Return array_pop ($ array );
- }
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:
- $ 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. |