- $this->load->helper (' name ');
Copy CodeWhere 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 ');
Copy CodeThe 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:
- $this->load->helper (Array (' Helper1 ', ' helper2 ', ' Helper3 '));
Copy Code4, 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:
- echo anchor (' Blog/comments ', ' Click here ');
- ?>
Copy CodeHere 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:
- 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 included in Array Helper, so it overrides the native function
- function Random_element ($array)
- {
- Shuffle ($array);
- Return Array_pop ($array);
- }
Copy Code7, 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:
- $config [' subclass_prefix '] = ' my_ ';
Copy CodeNote: Since all CodeIgniter libraries are named with prefixes such as ci_, do not use ci_ to customize the prefix. |