From cfe8b0986eac213508f45da346169933b45f8048 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 24 Oct 2023 08:46:27 +0900 Subject: [PATCH 1/5] docs: add new section titles "Confirming Namespaces" and "Application Namespace" --- user_guide_src/source/concepts/autoloader.rst | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/user_guide_src/source/concepts/autoloader.rst b/user_guide_src/source/concepts/autoloader.rst index 22722716661b..1a354208bf17 100644 --- a/user_guide_src/source/concepts/autoloader.rst +++ b/user_guide_src/source/concepts/autoloader.rst @@ -42,8 +42,10 @@ Configuration Initial configuration is done in **app/Config/Autoload.php**. This file contains two primary arrays: one for the classmap, and one for PSR-4 compatible namespaces. +.. _autoloader-namespaces: + Namespaces -********** +========== The recommended method for organizing your classes is to create one or more namespaces for your 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: The key of each row is the namespace itself. This does not need a trailing back slash. The value is the location to the directory the classes can be found in. -.. note:: You can check the namespace configuration by ``spark namespaces`` command: +.. _confirming-namespaces: + +Confirming Namespaces +===================== + +You can check the namespace configuration by ``spark namespaces`` command: + +.. code-block:: console - .. code-block:: console + php spark namespaces - php spark namespaces +Application Namespace +===================== By default, the application directory is namespace to the ``App`` namespace. You must namespace the controllers, 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 namespace has changed. Classmap -******** +======== The classmap is used extensively by CodeIgniter to eke the last ounces of performance out of the system by not hitting the file-system with extra ``is_file()`` calls. You can use the classmap to link to From 1ab584536e4785e406d270d2a294e28ba470a55c Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 24 Oct 2023 08:48:12 +0900 Subject: [PATCH 2/5] docs: add link to function page --- user_guide_src/source/general/modules.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/user_guide_src/source/general/modules.rst b/user_guide_src/source/general/modules.rst index 2a6df4aa7b2e..c2173792ab4a 100644 --- a/user_guide_src/source/general/modules.rst +++ b/user_guide_src/source/general/modules.rst @@ -240,8 +240,9 @@ For Windows: Helpers ======= -Helpers will be automatically discovered within defined namespaces when using the ``helper()`` function, as long as it -is within the namespaces **Helpers** directory: +Helpers will be automatically discovered within defined namespaces when using the +:php:func:`helper()` function, as long as it is within the namespaces **Helpers** +directory: .. literalinclude:: modules/009.php From bb1369a231ff00d8f3fd8230b45519631f9fe9fc Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 24 Oct 2023 08:48:32 +0900 Subject: [PATCH 3/5] docs: add link to "Helper Functions" --- user_guide_src/source/helpers/index.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/user_guide_src/source/helpers/index.rst b/user_guide_src/source/helpers/index.rst index 7efa4cd069fb..82c8c992a0bf 100644 --- a/user_guide_src/source/helpers/index.rst +++ b/user_guide_src/source/helpers/index.rst @@ -3,6 +3,7 @@ Helpers ####### Helpers are collections of useful procedural functions. +See also :doc:`../general/helpers`. .. toctree:: :glob: From fa31844349cd568e290731fa0e6dfe7136ba5d35 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 24 Oct 2023 08:49:19 +0900 Subject: [PATCH 4/5] docs: fix description for helper loading and add "Auto-Discovery and Composer Packages" --- user_guide_src/source/general/helpers.rst | 41 ++++++++++++++++------- user_guide_src/source/general/modules.rst | 2 +- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/user_guide_src/source/general/helpers.rst b/user_guide_src/source/general/helpers.rst index 6eadb9be6957..88accffd47b3 100644 --- a/user_guide_src/source/general/helpers.rst +++ b/user_guide_src/source/general/helpers.rst @@ -28,17 +28,17 @@ in your :doc:`controller <../incoming/controllers>` and :doc:`views <../outgoing/views>`. Helpers are typically stored in your **system/Helpers**, or -**app/Helpers** directory. CodeIgniter will look first in your -**app/Helpers** directory. If the directory does not exist or the -specified helper is not located there CI will instead look in your -global **system/Helpers** directory. +**app/Helpers** directory. -**************** -Loading a Helper -**************** +*************** +Loading Helpers +*************** .. note:: The URL helper is always loaded so you do not need to load it yourself. +Loading a Helper +================ + Loading a helper file is quite simple using the following method: .. literalinclude:: helpers/001.php @@ -58,6 +58,23 @@ For example, to load the **Cookie Helper** file, which is named .. note:: The Helper loading method above does not return a value, so don't try to assign it to a variable. Just use it as shown. +Auto-Discovery and Composer Packages +------------------------------------ + +By default, CodeIgniter will search for the helper files in all defined namespaces +by :ref:`auto-discovery`. +You can check your defined namespaces by the spark command. See :ref:`confirming-namespaces`. + +If you use many Composer packages, you will have many defined namespaces. +CodeIgniter will scan all namespaces by default. + +To avoid wasting time scanning for irrelevant Composer packages, you can manually +specify packages for Auto-Discovery. See :ref:`modules-specify-composer-packages` +for details. + +Or you can :ref:`specify a namespace ` +for a helper that you want to load. + Loading Multiple Helpers ======================== @@ -81,14 +98,14 @@ it. However if you want to load in your controller constructor, you can use the ``$helpers`` property in Controller instead. See :ref:`Controllers `. -.. _helpers-loading-from-non-standard-locations: +.. _helpers-loading-from-specified-namespace: -Loading from Non-standard Locations -=================================== +Loading from Specified Namespace +================================ Helpers can be loaded from directories outside of **app/Helpers** and -**system/Helpers**, as long as that path can be found through a namespace that -has been set up within the PSR-4 section of the :doc:`Autoloader config file <../concepts/autoloader>`. +**system/Helpers**, as long as that path can be found in defined namespaces. + You would prefix the name of the Helper with the namespace that it can be located in. Within that namespaced directory, the loader expects it to live within a sub-directory named **Helpers**. An example will help understand this. diff --git a/user_guide_src/source/general/modules.rst b/user_guide_src/source/general/modules.rst index c2173792ab4a..4e5c72a9fa9b 100644 --- a/user_guide_src/source/general/modules.rst +++ b/user_guide_src/source/general/modules.rst @@ -246,7 +246,7 @@ directory: .. literalinclude:: modules/009.php -You can specify namespaces. See :ref:`helpers-loading-from-non-standard-locations` for details. +You can specify namespaces. See :ref:`helpers-loading-from-specified-namespace` for details. Language Files ============== From 59ae2e62ed33028431ae14e37db1856a20950de3 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 24 Oct 2023 08:49:59 +0900 Subject: [PATCH 5/5] docs: fix typo in doc comment --- system/Common.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Common.php b/system/Common.php index ebfd8e2565e8..80dca72c74e3 100644 --- a/system/Common.php +++ b/system/Common.php @@ -579,7 +579,7 @@ function function_usable(string $functionName): bool if (! function_exists('helper')) { /** * Loads a helper file into memory. Supports namespaced helpers, - * both in and out of the 'helpers' directory of a namespaced directory. + * both in and out of the 'Helpers' directory of a namespaced directory. * * Will load ALL helpers of the matching name, in the following order: * 1. app/Helpers