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 |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 26 | global *system/helpers/* directory. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 27 | |
| 28 | Loading a Helper |
| 29 | ================ |
| 30 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 31 | Loading a helper file is quite simple using the following method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 43 | A helper can be loaded anywhere within your controller methods (or |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 50 | .. note:: The Helper loading method above does not return a value, so |
| 51 | don't try to assign it to a variable. Just use it as shown. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 59 | $this->load->helper( |
| 60 | array('helper1', 'helper2', 'helper3') |
| 61 | ); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | |
| 63 | Auto-loading Helpers |
| 64 | ==================== |
| 65 | |
| 66 | If you find that you need a particular helper globally throughout your |
| 67 | application, you can tell CodeIgniter to auto-load it during system |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 68 | initialization. This is done by opening the **application/config/autoload.php** |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 69 | file and adding the helper to the autoload array. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 70 | |
| 71 | Using a Helper |
| 72 | ============== |
| 73 | |
| 74 | Once you've loaded the Helper File containing the function you intend to |
| 75 | use, you'll call it the way you would a standard PHP function. |
| 76 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 77 | For example, to create a link using the ``anchor()`` function in one of |
| 78 | your view files you would do this:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 79 | |
| 80 | <?php echo anchor('blog/comments', 'Click Here');?> |
| 81 | |
| 82 | Where "Click Here" is the name of the link, and "blog/comments" is the |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 83 | URI to the controller/method you wish to link to. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 84 | |
| 85 | "Extending" Helpers |
| 86 | =================== |
| 87 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 88 | To "extend" Helpers, create a file in your **application/helpers/** folder |
| 89 | with an identical name to the existing Helper, but prefixed with **MY\_** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 90 | (this item is configurable. See below.). |
| 91 | |
| 92 | If all you need to do is add some functionality to an existing helper - |
| 93 | perhaps add a function or two, or change how a particular helper |
| 94 | function operates - then it's overkill to replace the entire helper with |
| 95 | your version. In this case it's better to simply "extend" the Helper. |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 96 | |
| 97 | .. note:: The term "extend" is used loosely since Helper functions are |
| 98 | procedural and discrete and cannot be extended in the traditional |
| 99 | programmatic sense. Under the hood, this gives you the ability to |
| 100 | add to or or to replace the functions a Helper provides. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 101 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 102 | For example, to extend the native **Array Helper** you'll create a file |
| 103 | named **application/helpers/MY_array_helper.php**, and add or override |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 104 | functions:: |
| 105 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 106 | // any_in_array() is not in the Array Helper, so it defines a new function |
| 107 | function any_in_array($needle, $haystack) |
| 108 | { |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 109 | $needle = is_array($needle) ? $needle : array($needle); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 110 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 111 | foreach ($needle as $item) |
| 112 | { |
| 113 | if (in_array($item, $haystack)) |
| 114 | { |
| 115 | return TRUE; |
| 116 | } |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 119 | return FALSE; |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // random_element() is included in Array Helper, so it overrides the native function |
| 123 | function random_element($array) |
| 124 | { |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 125 | shuffle($array); |
| 126 | return array_pop($array); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 127 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | |
| 129 | Setting Your Own Prefix |
| 130 | ----------------------- |
| 131 | |
| 132 | The filename prefix for "extending" Helpers is the same used to extend |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 133 | libraries and core classes. To set your own prefix, open your |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 134 | **application/config/config.php** file and look for this item:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 135 | |
| 136 | $config['subclass_prefix'] = 'MY_'; |
| 137 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 138 | Please note that all native CodeIgniter libraries are prefixed with **CI\_** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 139 | so DO NOT use that as your prefix. |
| 140 | |
| 141 | Now What? |
| 142 | ========= |
| 143 | |
| 144 | In the Table of Contents you'll find a list of all the available Helper |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 145 | Files. Browse each one to see what they do. |