Composer dependency manager for PHP

In this article we are going to introduce Composer, the PHP dependency manager, an essential tool for developing applications with PHP.

Composer is a project dependency manager for PHP programming. That means that it allows us to manage (declare, download and keep updated) the software packages on which our PHP project is based. It has become a go-to tool for any developer in this language who appreciates their time and agile development.

Are you starting a new project with PHP? Take a look at Composer beforehand because it can help you a lot at startup and thanks to it you can summarize many of the maintenance tasks of the third-party libraries you are using.

In this article we summarize the details to understand what Composer is, how it works and how to install it on your system. First we will start by explaining what a dependency manager is, then we will see how Composer works to realize what aspects of our day to day it will simplify for us. Finally we will see how to install it and how to use libraries managed with Composer in our PHP code.

Why a dependency manager

When you start a project in PHP, already of a certain complexity, it is not enough for you only with the native PHP function library. Generally, we all use some other library from third-party developers, which allows us to avoid starting everything from scratch. Whether it’s a framework or something narrower like a system for debugging or sending emails, form validation, etc., anything you might need is already created by other developers. If you’re not using any library you’re probably wasting your precious time, but that’s another discussion.

So, starting the project until now we had to go to the page for each of the software components we wanted to use, download them, copy them into our project folder, etc. Not only that, when we are in the middle of development, or already in production, and the version of the library changes, we have to manually download it again, update the files, etc. No one has ever died doing all that kind of setup and maintenance, but it sure takes us a while.

See also  Modify the Hosts file on Windows, Linux and Mac

All of this without taking into account that certain software, such as a framework like Symfony, depends in turn on many other libraries that you would have to install by hand and, in turn, keep updated.

Package managers help us to summarize the tasks of downloading and maintaining project versions so that they are always up to date. They already existed in other programming languages ​​and we found them especially useful as npm in NodeJS. Now PHP developers also have this tool thanks to Composer.

How Composer works

Composer allows us to declare the libraries that we want to use in a project. Its use is extremely simple, which encourages anyone to use it, whatever their technical level.

To benefit from the workflow that Composer proposes, we simply have to write a configuration file in which we indicate which packages we are going to require. The file is a simple JSON in which we indicate things like the author of the project, dependencies, etc.

The JSON file must have a specific name: composer.json

Below is an example of JSON where we declare various parameters of our application.

{ “name”: “webdevelopment/testing-composer”, “require”: { “phpmailer/phpmailer”: “5.2.*”, } }

Then we will start to break down this code so that each of its parts is understood, as well as we will see what other information we can place in this JSON. The idea is to see how easy it is to declare what libraries or software you are using and thus leave our project ready for the “magic” of Composer.

Once we have defined the dependencies in our project we must install them. We achieve this with a simple command in the terminal in which we ask Composer to install them:

See also  Error in count statement with mysql

composer install

Note: That command may vary depending on the installation you have on your Composer system. We will specify in a future article various situations in which we have to generate variants of this same command. For now we are going to stay in a presentation of Composer, but right away we start learning in detail here at .com

Once this command is launched, Composer will take care of going to the software package repositories and downloading those libraries mentioned, copying them to your project folder.

Once the process is finished in your command console you will be able to find in your project folder a directory called “vendor” where the declared libraries will be. Now we only have to make the includes so that they are available in your applications and for this Composer also helps us.

We will simply have to do a single include or require in our code and all the libraries will be available to use.

require ‘vendor/autoload.php’;

Packagist

To finish convincing you and tell you the complete introduction, you should take a look at Packagist. This is the repository of packages that are installable through Composer.

On the Packagist page you will find a search engine that can give you an idea of ​​the amount of material that you find available to use in any PHP project.

Simply search for any concept that interests you, such as email, template, wysiwyg, etc. You will see that several options appear classified by popularity, downloads, etc. Also on each package you will find information and the necessary code to declare your dependency on the Composer JSON.

See also  Front page for beginners

conclusion

I hope that with what we have seen so far this tool has caught your attention. The truth is that it is very useful and as we said, once you start using it you realize all the work it takes out of your way, not only in downloading packages, but also in updating libraries with the command ” composer update” which we will see later.

We know that we have left many things in the dark, such as the installation process and the details of the JSON, but we will see it in future articles. For now we wanted to introduce you to the dependency manager and let you know why we PHP developers have embraced it with such enthusiasm.

In the next article we will detail the process.

In addition to the upcoming articles where we are going to explain the details of the workflow with Composer for dependency management, we are now going to present you a video from our YouTube channel where you can find a summary of some of the basic steps of using Composer.

Loading Facebook Comments ...
Loading Disqus Comments ...