How to configure Autoload Composer?

Asked

Viewed 553 times

5

I am facing a lot with this autoload in php, I decided to study about it, I could understand a good part, but I have only one very specific doubt.

Inside the archive composer.json, in the stretch:

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

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

and its value? " App/".

1 answer

4


The specification of autoload of PSR-4 describes how the class should be loaded within your project. In Composer when defining the 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 that will be loaded to have this kind of "prefix".

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

Some other examples can be seen in specification of PSR-4

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.