PHP is powerful and versatile. However, because of its simplicity, it is easy to create messy code. This problem gets bigger as your application gets bigger. In fact, it will have separate modules and parts that work together. Furthermore, you are likely to rely on some third-party libraries. Composer is the solution to that. This powerful tool allows you to manage the dependencies of your project in a neat way. In this PHP Composer Tutorial, we showcase how to install and use omposer for that. Your PHP projects will never be the same after that!
PHP Composer Tutorial
Getting Composer
In the end, composer is a command-line utility you call from the terminal. It doesn’t come with PHP, so if you already have PHP you still need to install it. The installation procedure is a little bit different depending on your OS, but here we will cover Windows and Linux. Regardless of the OS, the first thing you need to do is downloading composer from the official website. Go to the download page, and here you will see the link for the Windows download.
On Windows, you get an .exe file you can run. It will ask where is your php.exe file (in C:\Software\XAMPP\php
if you followed our tutorial on how to install PHP on Windows). This is it, you are now ready to use Composer!
If you are on Linux, you can do all the installation – including the download – from the Terminal, and using PHP. Just run the following code.
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php composer-setup.php --install-dir=/dir/where/you/want/composer
php -r "unlink('composer-setup.php');"
And this is it! In case you want to be sure, the official website also offers the MD5 of the file so you know your composer is not corrupted.
Introducing Composer
Open the prompt and type composer
. You will see the (extensive) help of composer. We won’t cover all the tiniest details about Composer in this PHP Composer Tutorial, and we will focus only on the things you need to do. However, it’s nice to take a look at the entire help.
You can see we have a lot of options here. However, we need only a few.
The first thing we want to do in this PHP Composer Tutorial is installing a dependency into our project. We can do that with composer require <dependency>
. For example, imagine we need Laravel for our project. We can simply type composer require laravel/laravel
(because laravel/laravel
is the actual name of the dependency). This will do two main things:
- Put the files from the dependency, in this case, Laravel, inside a
vendor
folder. In this folder does not exist, composer will create it for you. All your dependencies will end up in thevendor
folder. - Add the dependency to the
composer.json
, creating that file if it does not exist. We will see what this is in a moment.
In case a package is not needed anymore, you can remove it from your project with composer remove <dependency>
. This will remove it from both vendor
folder and composer.json
.
Composer.json to manage dependencies
Composer works in the folder where you execute it. Typically, you want to do that in your project folder, because both vendor
and composer.json
are project-specific. Okay, but what is composer.json
? It is simply a file in your project root that keeps track of all the dependencies you have installed in your project. This is important because you don’t want to share the entire vendor
folder when you share your project or put it on a server. Instead, you share composer.json
and tell the server to install all the dependencies. In fact, the vendor
folder will not even be part of your source control (e.g. Git).
How to tell a device to install all the dependencies from composer.json
? Simply run composer install
in the folder where composer.json is.
Wrapping it up
In this PHP Composer Tutorial, we saw how to use composer to manage the dependencies of your PHP project. Installing Composer is very easy, you can do with a Next-Next setup on Windows, or with three CLI commands on Linux. Then, using it is even easier. You basically need to remember only three commands:
- Add a dependency to your project with
composer require <dependency>
- Remove a dependency from your project with
composer remove <dependency>
- Install all the dependencies of your project on your production server with
composer install
Hopefully, this PHP Composer Tutorial will give you some hints on how to make your PHP project better and more maintainable. What do you think of Composer? Is it improving your workflow? Let me know your opinions in the comments.