@@ -10,11 +10,13 @@ Helper Functions
1010What are Helpers?
1111*****************
1212
13- Helpers, as the name suggests, help you with tasks. Each helper file is
14- simply a collection of functions in a particular category. There are **URL
15- Helpers **, that assist in creating links, there are **Form Helpers ** that help
16- you create form elements, **Text Helpers ** perform various text formatting
17- routines, **Cookie Helpers ** set and read cookies, **File Helpers ** help you
13+ Helpers, as the name suggests, help you with tasks. Each helper file is simply a
14+ collection of functions in a particular category. There are
15+ :doc: `URL Helpers <../helpers/url_helper >`,
16+ that assist in creating links, there are :doc: `Form Helpers <../helpers/form_helper >`
17+ that help you create form elements, :doc: `Test Helpers <../helpers/text_helper >`
18+ perform various text formatting routines, :doc: `Cookie Helpers <../helpers/cookie_helper >`
19+ set and read cookies, :doc: `Filesystem Helpers <../helpers/filesystem_helper >` help you
1820deal with files, etc.
1921
2022Unlike most other systems in CodeIgniter, Helpers are not written in an
@@ -34,7 +36,7 @@ Helpers are typically stored in your **system/Helpers**, or
3436Loading Helpers
3537***************
3638
37- .. note :: The URL helper is always loaded so you do not need to load it yourself.
39+ .. note :: The :doc:`../helpers/url_helper` is always loaded so you do not need to load it yourself.
3840
3941Loading a Helper
4042================
@@ -43,19 +45,18 @@ Loading a helper file is quite simple using the following method:
4345
4446.. literalinclude :: helpers/001.php
4547
46- Where ``name `` is the file name of the helper, without the "**.php **" file
47- extension or the "**_helper **" part.
48+ The above code loads the **name_helper.php ** file.
4849
4950.. important :: CodeIgniter helper file names are all lowercase.
5051 Therefore, ``helper('Name') `` will not work on case-sensitive file systems
5152 such as Linux.
5253
53- For example, to load the ** Cookie Helper ** file, which is named
54+ For example, to load the :doc: ` ../helpers/cookie_helper ` file, which is named
5455**cookie_helper.php **, you would do this:
5556
5657.. literalinclude :: helpers/002.php
5758
58- .. note :: The Helper loading method above does not return a value, so
59+ .. note :: The :php:func:`helper()` function does not return a value, so
5960 don't try to assign it to a variable. Just use it as shown.
6061
6162Auto-Discovery and Composer Packages
@@ -75,6 +76,19 @@ for details.
7576Or you can :ref: `specify a namespace <helpers-loading-from-specified-namespace >`
7677for a helper that you want to load.
7778
79+ Load Order
80+ ----------
81+
82+ The :php:func: `helper() ` function will scan through all defined namespaces and
83+ load in ALL matching helpers of the same name. This allows any module's helpers
84+ to be loaded, as well as any helpers you've created specifically for this application.
85+
86+ The load order is as follows:
87+
88+ 1. app/Helpers - Files loaded here are always loaded first.
89+ 2. {namespace}/Helpers - All namespaces are looped through in the order they are defined.
90+ 3. system/Helpers - The base file is loaded last.
91+
7892Loading Multiple Helpers
7993========================
8094
@@ -103,12 +117,13 @@ property in Controller instead. See :ref:`Controllers <controllers-helpers>`.
103117Loading from Specified Namespace
104118================================
105119
106- Helpers can be loaded from directories outside of ** app/Helpers ** and
107- ** system/Helpers **, as long as that path can be found in defined namespaces .
120+ By default, CodeIgniter will search for the helper files in all defined namespaces
121+ and load all found files .
108122
109- You would prefix the name of the Helper with the namespace that it can be located
110- in. Within that namespaced directory, the loader expects it to live within a
111- sub-directory named **Helpers **. An example will help understand this.
123+ If you want to load only a helper in a specific namespace, prefix the name of the
124+ helper with the namespace that it can be located in. Within that namespaced directory,
125+ the loader expects it to live within a sub-directory named **Helpers **. An example
126+ will help understand this.
112127
113128For this example, assume that we have grouped together all of our Blog-related
114129code into its own namespace, ``Example\Blog ``. The files exist on our server at
@@ -153,11 +168,26 @@ your view files you would do this:
153168Where ``Click Here `` is the name of the link, and ``blog/comments `` is the
154169URI to the controller/method you wish to link to.
155170
156- *******************
171+ ****************
172+ Creating Helpers
173+ ****************
174+
175+ Creating Custom Helpers
176+ =======================
177+
178+ The helper filename is the **helper name ** and **_helper.php **.
179+
180+ For example, to create info helper, you’ll create a file named
181+ **app/Helpers/info_helper.php **, and add a function to the file:
182+
183+ .. literalinclude :: helpers/008.php
184+
185+ You can add as many functions as you like to a single helper file.
186+
157187"Extending" Helpers
158- *******************
188+ ===================
159189
160- To "extend" Helpers, create a file in your **app/Helpers/ ** folder
190+ To "extend" Helpers, create a file in your **app/Helpers ** folder
161191with an identical name to the existing Helper.
162192
163193If all you need to do is add some functionality to an existing helper -
@@ -178,14 +208,7 @@ functions:
178208
179209.. important :: Do not specify the namespace ``App\Helpers``.
180210
181- The :php:func: `helper() ` function will scan through all PSR-4 namespaces defined in **app/Config/Autoload.php **
182- and load in ALL matching helpers of the same name. This allows any module's helpers
183- to be loaded, as well as any helpers you've created specifically for this application. The load order
184- is as follows:
185-
186- 1. app/Helpers - Files loaded here are always loaded first.
187- 2. {namespace}/Helpers - All namespaces are looped through in the order they are defined.
188- 3. system/Helpers - The base file is loaded last
211+ See `Load Order `_ for the order in which helper files are loaded.
189212
190213*********
191214Now What?
0 commit comments