blob: 3a98311a679cd266cad62ea8c83c7571980ba25d [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
26global system/helpers folder.
27
28Loading a Helper
29================
30
31Loading a helper file is quite simple using the following function::
32
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
43A helper can be loaded anywhere within your controller functions (or
44even 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
50Note: The Helper loading function above does not return a value, so
51don't try to assign it to a variable. Just use it as shown.
52
53Loading Multiple Helpers
54========================
55
56If you need to load more than one helper you can specify them in an
57array, like this::
58
59 $this->load->helper( array('helper1', 'helper2', 'helper3') );
60
61Auto-loading Helpers
62====================
63
64If you find that you need a particular helper globally throughout your
65application, you can tell CodeIgniter to auto-load it during system
purwandi89f6f1a2011-10-07 19:58:22 +070066initialization. This is done by opening the **application/config/autoload.php**
67file and adding the helper to the autoload array.
Derek Jones8ede1a22011-10-05 13:34:52 -050068
69Using a Helper
70==============
71
72Once you've loaded the Helper File containing the function you intend to
73use, you'll call it the way you would a standard PHP function.
74
75For example, to create a link using the anchor() function in one of your
76view files you would do this::
77
78 <?php echo anchor('blog/comments', 'Click Here');?>
79
80Where "Click Here" is the name of the link, and "blog/comments" is the
81URI to the controller/function you wish to link to.
82
83"Extending" Helpers
84===================
85
purwandi89f6f1a2011-10-07 19:58:22 +070086To "extend" Helpers, create a file in your **application/helpers/** folder
87with an identical name to the existing Helper, but prefixed with **MY\_**
Derek Jones8ede1a22011-10-05 13:34:52 -050088(this item is configurable. See below.).
89
90If all you need to do is add some functionality to an existing helper -
91perhaps add a function or two, or change how a particular helper
92function operates - then it's overkill to replace the entire helper with
93your version. In this case it's better to simply "extend" the Helper.
94The term "extend" is used loosely since Helper functions are procedural
95and discrete and cannot be extended in the traditional programmatic
96sense. Under the hood, this gives you the ability to add to the
97functions a Helper provides, or to modify how the native Helper
98functions operate.
99
purwandi89f6f1a2011-10-07 19:58:22 +0700100For example, to extend the native **Array Helper** you'll create a file
101named **application/helpers/MY_array_helper.php**, and add or override
Derek Jones8ede1a22011-10-05 13:34:52 -0500102functions::
103
Derek Jonesa1360ef2011-10-05 17:22:53 -0500104 // 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 Jones8ede1a22011-10-05 13:34:52 -0500126
127Setting Your Own Prefix
128-----------------------
129
130The filename prefix for "extending" Helpers is the same used to extend
131libraries and Core classes. To set your own prefix, open your
purwandi89f6f1a2011-10-07 19:58:22 +0700132**application/config/config.php** file and look for this item::
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
134 $config['subclass_prefix'] = 'MY_';
135
purwandi89f6f1a2011-10-07 19:58:22 +0700136Please note that all native CodeIgniter libraries are prefixed with **CI\_**
Derek Jones8ede1a22011-10-05 13:34:52 -0500137so DO NOT use that as your prefix.
138
139Now What?
140=========
141
142In the Table of Contents you'll find a list of all the available Helper
143Files. Browse each one to see what they do.