blob: d171aa8edaed8a423861521086c4e500b86d076c [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001################
2Helper Functions
3################
4
5Helpers, as the name suggests, help you with tasks. Each helper file is
purwandi89f6f1a2011-10-07 19:58:22 +07006simply a collection of functions in a particular category. There are **URL
7Helpers**, that assist in creating links, there are Form Helpers that help
8you create form elements, **Text Helpers** perform various text formatting
9routines, **Cookie Helpers** set and read cookies, File Helpers help you
Derek Jones8ede1a22011-10-05 13:34:52 -050010deal with files, etc.
11
12Unlike most other systems in CodeIgniter, Helpers are not written in an
13Object Oriented format. They are simple, procedural functions. Each
14helper function performs one specific task, with no dependence on other
15functions.
16
17CodeIgniter does not load Helper Files by default, so the first step in
18using a Helper is to load it. Once loaded, it becomes globally available
19in your :doc:`controller <../general/controllers>` and
20:doc:`views <../general/views>`.
21
purwandi89f6f1a2011-10-07 19:58:22 +070022Helpers 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 Jones8ede1a22011-10-05 13:34:52 -050025specified helper is not located there CI will instead look in your
Andrey Andreev16a704c2012-11-09 17:25:00 +020026global *system/helpers/* directory.
Derek Jones8ede1a22011-10-05 13:34:52 -050027
28Loading a Helper
29================
30
Andrey Andreev16a704c2012-11-09 17:25:00 +020031Loading a helper file is quite simple using the following method::
Derek Jones8ede1a22011-10-05 13:34:52 -050032
33 $this->load->helper('name');
34
purwandi89f6f1a2011-10-07 19:58:22 +070035Where **name** is the file name of the helper, without the .php file
Derek Jones8ede1a22011-10-05 13:34:52 -050036extension or the "helper" part.
37
purwandi89f6f1a2011-10-07 19:58:22 +070038For example, to load the **URL Helper** file, which is named
39**url_helper.php**, you would do this::
Derek Jones8ede1a22011-10-05 13:34:52 -050040
41 $this->load->helper('url');
42
Andrey Andreev16a704c2012-11-09 17:25:00 +020043A helper can be loaded anywhere within your controller methods (or
Derek Jones8ede1a22011-10-05 13:34:52 -050044even within your View files, although that's not a good practice), as
45long as you load it before you use it. You can load your helpers in your
46controller constructor so that they become available automatically in
47any function, or you can load a helper in a specific function that needs
48it.
49
Andrey Andreev16a704c2012-11-09 17:25:00 +020050.. 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 Jones8ede1a22011-10-05 13:34:52 -050052
53Loading Multiple Helpers
54========================
55
56If you need to load more than one helper you can specify them in an
57array, like this::
58
Andrey Andreev16a704c2012-11-09 17:25:00 +020059 $this->load->helper(
60 array('helper1', 'helper2', 'helper3')
61 );
Derek Jones8ede1a22011-10-05 13:34:52 -050062
63Auto-loading Helpers
64====================
65
66If you find that you need a particular helper globally throughout your
67application, you can tell CodeIgniter to auto-load it during system
Andrey Andreev16a704c2012-11-09 17:25:00 +020068initialization. This is done by opening the **application/config/autoload.php**
purwandi89f6f1a2011-10-07 19:58:22 +070069file and adding the helper to the autoload array.
Derek Jones8ede1a22011-10-05 13:34:52 -050070
71Using a Helper
72==============
73
74Once you've loaded the Helper File containing the function you intend to
75use, you'll call it the way you would a standard PHP function.
76
Andrey Andreev16a704c2012-11-09 17:25:00 +020077For example, to create a link using the ``anchor()`` function in one of
78your view files you would do this::
Derek Jones8ede1a22011-10-05 13:34:52 -050079
80 <?php echo anchor('blog/comments', 'Click Here');?>
81
82Where "Click Here" is the name of the link, and "blog/comments" is the
Andrey Andreev16a704c2012-11-09 17:25:00 +020083URI to the controller/method you wish to link to.
Derek Jones8ede1a22011-10-05 13:34:52 -050084
85"Extending" Helpers
86===================
87
purwandi89f6f1a2011-10-07 19:58:22 +070088To "extend" Helpers, create a file in your **application/helpers/** folder
89with an identical name to the existing Helper, but prefixed with **MY\_**
Derek Jones8ede1a22011-10-05 13:34:52 -050090(this item is configurable. See below.).
91
92If all you need to do is add some functionality to an existing helper -
93perhaps add a function or two, or change how a particular helper
94function operates - then it's overkill to replace the entire helper with
95your version. In this case it's better to simply "extend" the Helper.
Andrey Andreev16a704c2012-11-09 17:25:00 +020096
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 Jones8ede1a22011-10-05 13:34:52 -0500101
purwandi89f6f1a2011-10-07 19:58:22 +0700102For example, to extend the native **Array Helper** you'll create a file
103named **application/helpers/MY_array_helper.php**, and add or override
Derek Jones8ede1a22011-10-05 13:34:52 -0500104functions::
105
Derek Jonesa1360ef2011-10-05 17:22:53 -0500106 // any_in_array() is not in the Array Helper, so it defines a new function
107 function any_in_array($needle, $haystack)
108 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200109 $needle = is_array($needle) ? $needle : array($needle);
Derek Jonesa1360ef2011-10-05 17:22:53 -0500110
Andrey Andreev16a704c2012-11-09 17:25:00 +0200111 foreach ($needle as $item)
112 {
113 if (in_array($item, $haystack))
114 {
115 return TRUE;
116 }
Derek Jonesa1360ef2011-10-05 17:22:53 -0500117 }
118
Andrey Andreev16a704c2012-11-09 17:25:00 +0200119 return FALSE;
Derek Jonesa1360ef2011-10-05 17:22:53 -0500120 }
121
122 // random_element() is included in Array Helper, so it overrides the native function
123 function random_element($array)
124 {
Andrey Andreev16a704c2012-11-09 17:25:00 +0200125 shuffle($array);
126 return array_pop($array);
Derek Jonesa1360ef2011-10-05 17:22:53 -0500127 }
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
129Setting Your Own Prefix
130-----------------------
131
132The filename prefix for "extending" Helpers is the same used to extend
Andrey Andreev16a704c2012-11-09 17:25:00 +0200133libraries and core classes. To set your own prefix, open your
purwandi89f6f1a2011-10-07 19:58:22 +0700134**application/config/config.php** file and look for this item::
Derek Jones8ede1a22011-10-05 13:34:52 -0500135
136 $config['subclass_prefix'] = 'MY_';
137
purwandi89f6f1a2011-10-07 19:58:22 +0700138Please note that all native CodeIgniter libraries are prefixed with **CI\_**
Derek Jones8ede1a22011-10-05 13:34:52 -0500139so DO NOT use that as your prefix.
140
141Now What?
142=========
143
144In the Table of Contents you'll find a list of all the available Helper
Andrey Andreev16a704c2012-11-09 17:25:00 +0200145Files. Browse each one to see what they do.