Skip to content

Commit

Permalink
Merge pull request #8666 from kenjis/docs-autoloader.rst
Browse files Browse the repository at this point in the history
docs: add and update Autoloader descriptions
  • Loading branch information
kenjis authored Mar 28, 2024
2 parents e22b0c5 + d99eba7 commit 1046de8
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions user_guide_src/source/concepts/autoloader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
*************

Expand All @@ -47,16 +50,23 @@ 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

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
Expand Down Expand Up @@ -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
****************

Expand Down

0 comments on commit 1046de8

Please sign in to comment.