22Helper Functions
33################
44
5+ .. contents ::
6+ :local:
7+ :depth: 2
8+
9+ *****************
10+ What are Helpers?
11+ *****************
12+
513Helpers, as the name suggests, help you with tasks. Each helper file is
614simply a collection of functions in a particular category. There are **URL
715Helpers **, that assist in creating links, there are **Form Helpers ** that help
816you create form elements, **Text Helpers ** perform various text formatting
917routines, **Cookie Helpers ** set and read cookies, **File Helpers ** help you
1018deal with files, etc.
1119
12- .. contents ::
13- :local:
14- :depth: 2
15-
1620Unlike most other systems in CodeIgniter, Helpers are not written in an
1721Object Oriented format. They are simple, procedural functions. Each
1822helper function performs one specific task, with no dependence on other
@@ -29,8 +33,9 @@ Helpers are typically stored in your **system/Helpers**, or
2933specified helper is not located there CI will instead look in your
3034global **system/Helpers ** directory.
3135
36+ ****************
3237Loading a Helper
33- ================
38+ ****************
3439
3540.. note :: The URL helper is always loaded so you do not need to load it yourself.
3641
@@ -41,6 +46,10 @@ Loading a helper file is quite simple using the following method:
4146Where ``name `` is the file name of the helper, without the "**.php **" file
4247extension or the "**_helper **" part.
4348
49+ .. important :: CodeIgniter helper file names are all lowercase.
50+ Therefore, ``helper('Name') `` will not work on case-sensitive file systems
51+ such as Linux.
52+
4453For example, to load the **Cookie Helper ** file, which is named
4554**cookie_helper.php **, you would do this:
4655
@@ -50,15 +59,15 @@ For example, to load the **Cookie Helper** file, which is named
5059 don't try to assign it to a variable. Just use it as shown.
5160
5261Loading Multiple Helpers
53- ------------------------
62+ ========================
5463
5564If you need to load more than one helper at a time, you can pass
5665an array of file names in and all of them will be loaded:
5766
5867.. literalinclude :: helpers/003.php
5968
6069Loading in a Controller
61- -----------------------
70+ =======================
6271
6372A helper can be loaded anywhere within your controller methods (or
6473even within your View files, although that's not a good practice), as
@@ -75,7 +84,7 @@ property in Controller instead. See :ref:`Controllers <controllers-helpers>`.
7584.. _helpers-loading-from-non-standard-locations :
7685
7786Loading from Non-standard Locations
78- -----------------------------------
87+ ===================================
7988
8089Helpers can be loaded from directories outside of **app/Helpers ** and
8190**system/Helpers **, as long as that path can be found through a namespace that
@@ -100,8 +109,9 @@ You can also use the following way:
100109.. note :: The functions within files loaded this way are not truly namespaced.
101110 The namespace is simply used as a convenient way to locate the files.
102111
112+ **************
103113Using a Helper
104- ==============
114+ **************
105115
106116Once you've loaded the Helper File containing the function you intend to
107117use, you'll call it the way you would a standard PHP function.
@@ -114,8 +124,9 @@ your view files you would do this:
114124Where ``Click Here `` is the name of the link, and ``blog/comments `` is the
115125URI to the controller/method you wish to link to.
116126
127+ *******************
117128"Extending" Helpers
118- ===================
129+ *******************
119130
120131To "extend" Helpers, create a file in your **app/Helpers/ ** folder
121132with an identical name to the existing Helper.
@@ -136,7 +147,9 @@ functions:
136147
137148.. literalinclude :: helpers/006.php
138149
139- The ``helper() `` function will scan through all PSR-4 namespaces defined in **app/Config/Autoload.php **
150+ .. important :: Do not specify the namespace ``App\Helpers``.
151+
152+ The :php:func: `helper() ` function will scan through all PSR-4 namespaces defined in **app/Config/Autoload.php **
140153and load in ALL matching helpers of the same name. This allows any module's helpers
141154to be loaded, as well as any helpers you've created specifically for this application. The load order
142155is as follows:
@@ -145,8 +158,9 @@ is as follows:
1451582. {namespace}/Helpers - All namespaces are looped through in the order they are defined.
1461593. system/Helpers - The base file is loaded last
147160
161+ *********
148162Now What?
149- =========
163+ *********
150164
151165In the Table of Contents, you'll find a list of all the available :doc: `Helpers <../helpers/index >`.
152166Browse each one to see what they do.
0 commit comments