How to configure Autoload Composer?

3

I'm looking for a lot of autoload in php, I decided to study about it, I managed to understand a good part, but I have a single specific doubt.

Within the file composer.json , in the excerpt:

"autoload": {
    "psr-4": {
        "App\" : "App/"
    }
}

I would like to know in "App\" what is this referring to within a folder structure of a project? namespaces? to a specific directory? to a class?

and its value? "App /".

    
asked by anonymous 30.03.2018 / 03:40

1 answer

2

The autoload specification of PSR-4 describes how the class should load into your project. In Composer when defining json we have these two parts:

{ "Namespace\Base\" : "diretorio/raiz" }

So by using this setting in your composer.json , you allow the rest of the files to be loaded to have this kind of "prefix".

Example: When you make a use Namespace\Base\Http\BaseController it will include the diretorio/raiz/http/BaseController.php file in your code.

Some other examples can be seen on the PSR-4 specification

Fully Qualified Class Name    | Namespace Prefix   | Base Directory           | Resulting File Path
----------------------------- |--------------------|--------------------------|-------------------------------------------
\Acme\Log\Writer\File_Writer  | Acme\Log\Writer    | ./acme-log-writer/lib/   | ./acme-log-writer/lib/File_Writer.php
\Aura\Web\Response\Status     | Aura\Web           | /path/to/aura-web/src/   | /path/to/aura-web/src/Response/Status.php
\Symfony\Core\Request         | Symfony\Core       | ./vendor/Symfony/Core/   | ./vendor/Symfony/Core/Request.php
\Zend\Acl                     | Zend               | /usr/includes/Zend/      | /usr/includes/Zend/Acl.php
    
30.03.2018 / 03:50