Skip to content

Commit b92057c

Browse files
authored
Merge pull request #8084 from kenjis/docs-fix-helper-loading
docs: fix helper loading
2 parents 130be04 + 59ae2e6 commit b92057c

File tree

5 files changed

+50
-21
lines changed

5 files changed

+50
-21
lines changed

system/Common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ function function_usable(string $functionName): bool
579579
if (! function_exists('helper')) {
580580
/**
581581
* Loads a helper file into memory. Supports namespaced helpers,
582-
* both in and out of the 'helpers' directory of a namespaced directory.
582+
* both in and out of the 'Helpers' directory of a namespaced directory.
583583
*
584584
* Will load ALL helpers of the matching name, in the following order:
585585
* 1. app/Helpers

user_guide_src/source/concepts/autoloader.rst

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ Configuration
4242
Initial configuration is done in **app/Config/Autoload.php**. This file contains two primary
4343
arrays: one for the classmap, and one for PSR-4 compatible namespaces.
4444

45+
.. _autoloader-namespaces:
46+
4547
Namespaces
46-
**********
48+
==========
4749

4850
The recommended method for organizing your classes is to create one or more namespaces for your
4951
application's files. This is most important for any business-logic related classes, entity classes,
@@ -55,11 +57,19 @@ those classes can be found in:
5557
The key of each row is the namespace itself. This does not need a trailing back slash.
5658
The value is the location to the directory the classes can be found in.
5759

58-
.. note:: You can check the namespace configuration by ``spark namespaces`` command:
60+
.. _confirming-namespaces:
61+
62+
Confirming Namespaces
63+
=====================
64+
65+
You can check the namespace configuration by ``spark namespaces`` command:
66+
67+
.. code-block:: console
5968
60-
.. code-block:: console
69+
php spark namespaces
6170
62-
php spark namespaces
71+
Application Namespace
72+
=====================
6373

6474
By default, the application directory is namespace to the ``App`` namespace. You must namespace the controllers,
6575
libraries, or models in the application directory, and they will be found under the ``App`` namespace.
@@ -77,7 +87,7 @@ You will need to modify any existing files that are referencing the current name
7787
namespace has changed.
7888

7989
Classmap
80-
********
90+
========
8191

8292
The classmap is used extensively by CodeIgniter to eke the last ounces of performance out of the system
8393
by not hitting the file-system with extra ``is_file()`` calls. You can use the classmap to link to

user_guide_src/source/general/helpers.rst

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ in your :doc:`controller <../incoming/controllers>` and
2828
:doc:`views <../outgoing/views>`.
2929

3030
Helpers are typically stored in your **system/Helpers**, or
31-
**app/Helpers** directory. CodeIgniter will look first in your
32-
**app/Helpers** directory. If the directory does not exist or the
33-
specified helper is not located there CI will instead look in your
34-
global **system/Helpers** directory.
31+
**app/Helpers** directory.
3532

36-
****************
37-
Loading a Helper
38-
****************
33+
***************
34+
Loading Helpers
35+
***************
3936

4037
.. note:: The URL helper is always loaded so you do not need to load it yourself.
4138

39+
Loading a Helper
40+
================
41+
4242
Loading a helper file is quite simple using the following method:
4343

4444
.. literalinclude:: helpers/001.php
@@ -58,6 +58,23 @@ For example, to load the **Cookie Helper** file, which is named
5858
.. note:: The Helper loading method above does not return a value, so
5959
don't try to assign it to a variable. Just use it as shown.
6060

61+
Auto-Discovery and Composer Packages
62+
------------------------------------
63+
64+
By default, CodeIgniter will search for the helper files in all defined namespaces
65+
by :ref:`auto-discovery`.
66+
You can check your defined namespaces by the spark command. See :ref:`confirming-namespaces`.
67+
68+
If you use many Composer packages, you will have many defined namespaces.
69+
CodeIgniter will scan all namespaces by default.
70+
71+
To avoid wasting time scanning for irrelevant Composer packages, you can manually
72+
specify packages for Auto-Discovery. See :ref:`modules-specify-composer-packages`
73+
for details.
74+
75+
Or you can :ref:`specify a namespace <helpers-loading-from-specified-namespace>`
76+
for a helper that you want to load.
77+
6178
Loading Multiple Helpers
6279
========================
6380

@@ -81,14 +98,14 @@ it.
8198
However if you want to load in your controller constructor, you can use the ``$helpers``
8299
property in Controller instead. See :ref:`Controllers <controllers-helpers>`.
83100

84-
.. _helpers-loading-from-non-standard-locations:
101+
.. _helpers-loading-from-specified-namespace:
85102

86-
Loading from Non-standard Locations
87-
===================================
103+
Loading from Specified Namespace
104+
================================
88105

89106
Helpers can be loaded from directories outside of **app/Helpers** and
90-
**system/Helpers**, as long as that path can be found through a namespace that
91-
has been set up within the PSR-4 section of the :doc:`Autoloader config file <../concepts/autoloader>`.
107+
**system/Helpers**, as long as that path can be found in defined namespaces.
108+
92109
You would prefix the name of the Helper with the namespace that it can be located
93110
in. Within that namespaced directory, the loader expects it to live within a
94111
sub-directory named **Helpers**. An example will help understand this.

user_guide_src/source/general/modules.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,13 @@ For Windows:
240240
Helpers
241241
=======
242242

243-
Helpers will be automatically discovered within defined namespaces when using the ``helper()`` function, as long as it
244-
is within the namespaces **Helpers** directory:
243+
Helpers will be automatically discovered within defined namespaces when using the
244+
:php:func:`helper()` function, as long as it is within the namespaces **Helpers**
245+
directory:
245246

246247
.. literalinclude:: modules/009.php
247248

248-
You can specify namespaces. See :ref:`helpers-loading-from-non-standard-locations` for details.
249+
You can specify namespaces. See :ref:`helpers-loading-from-specified-namespace` for details.
249250

250251
Language Files
251252
==============

user_guide_src/source/helpers/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Helpers
33
#######
44

55
Helpers are collections of useful procedural functions.
6+
See also :doc:`../general/helpers`.
67

78
.. toctree::
89
:glob:

0 commit comments

Comments
 (0)