Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################ |
| 2 | Helper Functions |
| 3 | ################ |
| 4 | |
| 5 | Helpers, as the name suggests, help you with tasks. Each helper file is |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 6 | simply a collection of functions in a particular category. There are **URL |
| 7 | Helpers**, that assist in creating links, there are Form Helpers that help |
| 8 | you create form elements, **Text Helpers** perform various text formatting |
| 9 | routines, **Cookie Helpers** set and read cookies, File Helpers help you |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 10 | deal with files, etc. |
| 11 | |
| 12 | Unlike most other systems in CodeIgniter, Helpers are not written in an |
| 13 | Object Oriented format. They are simple, procedural functions. Each |
| 14 | helper function performs one specific task, with no dependence on other |
| 15 | functions. |
| 16 | |
| 17 | CodeIgniter does not load Helper Files by default, so the first step in |
| 18 | using a Helper is to load it. Once loaded, it becomes globally available |
| 19 | in your :doc:`controller <../general/controllers>` and |
| 20 | :doc:`views <../general/views>`. |
| 21 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 22 | Helpers are typically stored in your **system/helpers**, or |
| 23 | **application/helpers directory**. CodeIgniter will look first in your |
| 24 | **application/helpers directory**. If the directory does not exist or the |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 25 | specified helper is not located there CI will instead look in your |
| 26 | global system/helpers folder. |
| 27 | |
| 28 | Loading a Helper |
| 29 | ================ |
| 30 | |
| 31 | Loading a helper file is quite simple using the following function:: |
| 32 | |
| 33 | $this->load->helper('name'); |
| 34 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 35 | Where **name** is the file name of the helper, without the .php file |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 36 | extension or the "helper" part. |
| 37 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 38 | For example, to load the **URL Helper** file, which is named |
| 39 | **url_helper.php**, you would do this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 40 | |
| 41 | $this->load->helper('url'); |
| 42 | |
| 43 | A helper can be loaded anywhere within your controller functions (or |
| 44 | even within your View files, although that's not a good practice), as |
| 45 | long as you load it before you use it. You can load your helpers in your |
| 46 | controller constructor so that they become available automatically in |
| 47 | any function, or you can load a helper in a specific function that needs |
| 48 | it. |
| 49 | |
| 50 | Note: The Helper loading function above does not return a value, so |
| 51 | don't try to assign it to a variable. Just use it as shown. |
| 52 | |
| 53 | Loading Multiple Helpers |
| 54 | ======================== |
| 55 | |
| 56 | If you need to load more than one helper you can specify them in an |
| 57 | array, like this:: |
| 58 | |
| 59 | $this->load->helper( array('helper1', 'helper2', 'helper3') ); |
| 60 | |
| 61 | Auto-loading Helpers |
| 62 | ==================== |
| 63 | |
| 64 | If you find that you need a particular helper globally throughout your |
| 65 | application, you can tell CodeIgniter to auto-load it during system |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 66 | initialization. This is done by opening the **application/config/autoload.php** |
| 67 | file and adding the helper to the autoload array. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | |
| 69 | Using a Helper |
| 70 | ============== |
| 71 | |
| 72 | Once you've loaded the Helper File containing the function you intend to |
| 73 | use, you'll call it the way you would a standard PHP function. |
| 74 | |
| 75 | For example, to create a link using the anchor() function in one of your |
| 76 | view files you would do this:: |
| 77 | |
| 78 | <?php echo anchor('blog/comments', 'Click Here');?> |
| 79 | |
| 80 | Where "Click Here" is the name of the link, and "blog/comments" is the |
| 81 | URI to the controller/function you wish to link to. |
| 82 | |
| 83 | "Extending" Helpers |
| 84 | =================== |
| 85 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 86 | To "extend" Helpers, create a file in your **application/helpers/** folder |
| 87 | with an identical name to the existing Helper, but prefixed with **MY\_** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | (this item is configurable. See below.). |
| 89 | |
| 90 | If all you need to do is add some functionality to an existing helper - |
| 91 | perhaps add a function or two, or change how a particular helper |
| 92 | function operates - then it's overkill to replace the entire helper with |
| 93 | your version. In this case it's better to simply "extend" the Helper. |
| 94 | The term "extend" is used loosely since Helper functions are procedural |
| 95 | and discrete and cannot be extended in the traditional programmatic |
| 96 | sense. Under the hood, this gives you the ability to add to the |
| 97 | functions a Helper provides, or to modify how the native Helper |
| 98 | functions operate. |
| 99 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 100 | For example, to extend the native **Array Helper** you'll create a file |
| 101 | named **application/helpers/MY_array_helper.php**, and add or override |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 102 | functions:: |
| 103 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 104 | // any_in_array() is not in the Array Helper, so it defines a new function |
| 105 | function any_in_array($needle, $haystack) |
| 106 | { |
| 107 | $needle = (is_array($needle)) ? $needle : array($needle); |
| 108 | |
| 109 | foreach ($needle as $item) |
| 110 | { |
| 111 | if (in_array($item, $haystack)) |
| 112 | { |
| 113 | return TRUE; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return FALSE; |
| 118 | } |
| 119 | |
| 120 | // random_element() is included in Array Helper, so it overrides the native function |
| 121 | function random_element($array) |
| 122 | { |
| 123 | shuffle($array); |
| 124 | return array_pop($array); |
| 125 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
| 127 | Setting Your Own Prefix |
| 128 | ----------------------- |
| 129 | |
| 130 | The filename prefix for "extending" Helpers is the same used to extend |
| 131 | libraries and Core classes. To set your own prefix, open your |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 132 | **application/config/config.php** file and look for this item:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 133 | |
| 134 | $config['subclass_prefix'] = 'MY_'; |
| 135 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 136 | Please note that all native CodeIgniter libraries are prefixed with **CI\_** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 137 | so DO NOT use that as your prefix. |
| 138 | |
| 139 | Now What? |
| 140 | ========= |
| 141 | |
| 142 | In the Table of Contents you'll find a list of all the available Helper |
| 143 | Files. Browse each one to see what they do. |