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
@@ -103,7 +112,7 @@ You can also use the following way:
103112.. _auto-loading-helpers :
104113
105114Auto-loading Helpers
106- --------------------
115+ ====================
107116
108117.. versionadded :: 4.3.0
109118
@@ -112,8 +121,9 @@ you can tell CodeIgniter to auto-load it during system initialization.
112121This is done by opening the **app/Config/Autoload.php ** file
113122and adding the helper to the ``$helpers `` property.
114123
124+ **************
115125Using a Helper
116- ==============
126+ **************
117127
118128Once you've loaded the Helper File containing the function you intend to
119129use, you'll call it the way you would a standard PHP function.
@@ -126,8 +136,9 @@ your view files you would do this:
126136Where ``Click Here `` is the name of the link, and ``blog/comments `` is the
127137URI to the controller/method you wish to link to.
128138
139+ *******************
129140"Extending" Helpers
130- ===================
141+ *******************
131142
132143To "extend" Helpers, create a file in your **app/Helpers/ ** folder
133144with an identical name to the existing Helper.
@@ -148,7 +159,9 @@ functions:
148159
149160.. literalinclude :: helpers/006.php
150161
151- The ``helper() `` function will scan through all PSR-4 namespaces defined in **app/Config/Autoload.php **
162+ .. important :: Do not specify the namespace ``App\Helpers``.
163+
164+ The :php:func: `helper() ` function will scan through all PSR-4 namespaces defined in **app/Config/Autoload.php **
152165and load in ALL matching helpers of the same name. This allows any module's helpers
153166to be loaded, as well as any helpers you've created specifically for this application. The load order
154167is as follows:
@@ -157,8 +170,9 @@ is as follows:
1571702. {namespace}/Helpers - All namespaces are looped through in the order they are defined.
1581713. system/Helpers - The base file is loaded last
159172
173+ *********
160174Now What?
161- =========
175+ *********
162176
163177In the Table of Contents, you'll find a list of all the available :doc: `Helpers <../helpers/index >`.
164178Browse each one to see what they do.
0 commit comments