Composer: how to autoload php classes outside the “vendor”

The Composer (https://getcomposer.org/) is a dependency manager (packages) widely used in language PHP and provides many facilities, since it is responsible for checking, download to engage our project all the “libs” of third parties that we will need.

These “libs”, when downloaded by Composer, are grouped within a directory called “vendor”. In addition, a file called “autoload.php” is also created in this folder, responsible for “loading” all the classes we need to use. Basically, all I have to do is import “autoload.php” into a file of my project and I will have access to all classes of “libs” downloaded by Composer.

Well, despite the many practicalities that we found when we chose to use Composer to aid in the development of our projects, a question that arises is: how to have an easy “autoload” also to import the classes of the project itself (which are those that we ourselves developed and that, therefore, are not included in the “vendor” folder)?

Before showing how to easily solve this problem, let’s think about the following folder structure for a hypothetical project:

  • src
    • Config
    • Database
    • Models
    • etc…
  • public
    • css
    • js
    • img
    • etc…
  • vendor

Good. So, basically, we have the folder “src”, where we create the classes of the project itself, which are particular to it. In the “public” folder , we put our “assets” files, such as images, javascripts and etc … And in the “vendor” folder , obviously, there are the “libs” imported by Composer.

As we saw a few paragraphs above, when we want to import a class, we call (include) the file “vendor/autoload.php” and then instantiate the classes through their “namespaces”. But what about when we want to import / use a class that we created ourselves (within a subfolder of “src”)? The “autoload” is configured to search for classes within the “vendor”, but we can add a configuration so that we can also add the “src” folder when doing this search.

File: “Models/BaseModel.php”
Namespace: “ProjetoHipotetico\Models\BaseModel”

So, just go to the file “composer.json”, which is at the root of your project and add the namespaces / folders that should be considered by the “autoload”:

1
2
3
4
5
6
7
8
{
    "autoload": {
        "psr-4": {
            "ProjetoHipotetico\\" : "src/", 
            "Vendor\\Namespace\\" : ""         
        }
    }
}

In line 4 we add the folder of our project that houses our classes and in line 5 it is the “default” search within the “libs” of the “vendor” folder.

After making this change, don’t forget to save the “composer.json” file and give your console a “composer update”.

From now on, “autoload.php” will serve both to import the classes installed by Composer and also to import the classes that you have developed for the project. Simple and easy.

I hope this one helps you =)

Hugs!

He has a degree in Information Systems, a postgraduate degree in Database Systems and a master's degree in Education with a focus on Sociocommunity Technologies. He works as a teacher of technical and technological education at the Federal Institute of Education, Science and Technology of São Paulo, teaching subjects in the areas of programming, database, project development and software engineering.

Posts relacionados

Leave a Reply

Your email address will not be published. Required fields are marked *