Merge pull request #8666 from kenjis/docs-autoloader.rst

docs: add and update Autoloader descriptions
This commit is contained in:
kenjis 2024-03-28 11:58:01 +09:00 committed by GitHub
commit 1046de86bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,13 +13,15 @@ classes that your project is using. Keeping track of where every single file is,
hard-coding that location into your files in a series of ``requires()`` is a massive
headache and very error-prone. That's where autoloaders come in.
***********************
CodeIgniter4 Autoloader
***********************
CodeIgniter provides a very flexible autoloader that can be used with very little configuration.
It can locate individual namespaced classes that adhere to
`PSR-4 <https://www.php-fig.org/psr/psr-4/>`_ autoloading
directory structures.
`PSR-4`_ autoloading directory structures.
.. _PSR-4: https://www.php-fig.org/psr/psr-4/
The autoloader works great by itself, but can also work with other autoloaders, like
`Composer <https://getcomposer.org>`_, or even your own custom autoloaders, if needed.
@ -36,6 +38,7 @@ beginning of the framework's execution.
file name case is incorrect, the autoloader cannot find the file on the
server.
*************
Configuration
*************
@ -47,9 +50,10 @@ arrays: one for the classmap, and one for PSR-4 compatible 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,
etc. The ``$psr4`` array in the configuration file allows you to map the namespace to the directory
The recommended method for organizing your classes is to create one or more namespaces
for your application's files.
The ``$psr4`` array in the configuration file allows you to map the namespace to the directory
those classes can be found in:
.. literalinclude:: autoloader/001.php
@ -57,6 +61,12 @@ 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.
By default, the namespace ``App`` is located in the **app** directory, and the
namespace ``Config`` is located in the ``app/Config`` directory.
If you create class files in the locations and according to `PSR-4`_, the autoloader
will autoload them.
.. _confirming-namespaces:
Confirming Namespaces
@ -89,14 +99,14 @@ You will need to modify any existing files that are referencing the current name
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
third-party libraries that are not namespaced:
If you use third-party libraries that are not Composer packages and are not namespaced,
you can load those classes using the classmap:
.. literalinclude:: autoloader/003.php
The key of each row is the name of the class that you want to locate. The value is the path to locate it at.
****************
Composer Support
****************