What to expect in the next generation of Laravel Forge. Read the blog post
Rewatch this lesson

Composer

PHP Fundamentals

Composer
Learn about PHP's package manager, how to manage dependencies, and set up projects professionally.


Composer is PHP's package manager and an essential tool for modern PHP development. It helps you manage your project's dependencies and autoload your classes. Almost every modern PHP project uses Composer - Laravel itself is installed via Composer, and popular packages like Guzzle (for HTTP requests), Carbon (for date handling), and PHPUnit (for testing) are all distributed through Composer.

You can find these packages and many more on Packagist.org, PHP's main package repository with over 300,000 packages available. Think of it as npm for PHP - it's where developers share and distribute their code.

Let's see how it works.

Installing Composer

First, you need Composer on your machine. If you followed our setup video, you already have it installed. You can verify this by running:

1composer --version

Creating a New Project

Every PHP project with Composer starts with a composer.json file. You can create one manually or use:

1composer init

This will walk you through a setup process to create your next composer project.

Installing Dependencies

To install a package, you can either:

1composer require guzzlehttp/guzzle

Or add it to composer.json and run:

1composer install

Autoloading

One of Composer's best features is autoloading. It automatically loads your PHP classes without requiring manual includes. You just have to tell composer about it. In your composer.json:

1{
2 "autoload": {
3 "psr-4": {
4 "App\\\\": "src/"
5 }
6 }
7}

Now your classes will be autoloaded:

1// src/User.php
2namespace App;
3 
4class User {
5 // Your code
6}

When you start working with Laravel, you'll see that Composer is used extensively for managing packages and autoloading. It's a fundamental tool that makes modern PHP development possible.

In fact, Laravel itself is installed via Composer, which we'll cover in our Laravel series!

00:00
Composer is PHP's package manager and an essential tool for modern development.
00:04
It helps you to manage your project's dependencies, it helps you with
00:08
autoloading your classes and almost every modern project uses Composer.
00:13
By the way, Level itself is installed via Composer and popular packages like
00:18
Gazelle, Carbon or PHPUnit are all distributed through Composer. You can find these
00:24
packages and many more on packages.org. So this is PHP's main package repository
00:31
with over 300,000 packages for you available and yeah think of it as NPM
00:37
for PHP. So whatever you need you're going to find it here as a dependency
00:42
for your project. In order to find out if you have Composer installed you can just
00:47
run Composer version but if you have used the methods which were shown you to
00:52
install PHP and Composer you should have it already on your machine.
00:56
I'm here in a brand new directory called Composer project so this will be our
01:01
example project for this video and we can start creating a new Composer
01:05
project with Composer in it. This will ask us a few questions and in the end
01:11
it will create a Composer.json file which holds all the information we
01:16
need in order for our application. So we have here a package name which consists
01:22
of the vendor normally that's just the name of the company and then the name
01:27
of the project. So in this case I can leave it with the default value which I
01:32
can just accept by click enter. The description, our new project and then the
01:41
author which is just me. Stability version we don't care. There are different
01:45
types of projects so there's a library which is something that you would
01:48
install into your project but in our case it's a project which we're
01:52
creating here so let's just stick with project which is the default one. You can
01:56
also define the license and yeah much more. So what we also can do is we can
02:01
define our dependencies and yeah let's just do this here for this demo
02:06
application. So what do we need as a dependency? One thing that many packages
02:11
need is the Gazelle HTTP client which helps you a lot to create HTTP calls to
02:17
specific services or talk to APIs. We have this at number zero so let's bring
02:23
this in. Yeah let's just use the latest version here and we are good for this
02:28
now and now we're going to get asked about our dev dependency. So these are
02:32
dependencies which we only need locally. So this means if we deploy our
02:36
application we don't need those dependencies there. So let's also add one
02:41
for this as well and the very common one is PHP unit which we use in PHP for
02:46
testing. Again we're just using here the latest version and I think we are good
02:51
to go. Compose unit also helps us to set up
02:55
auto loading which is something which is very important when working with PHP
02:58
classes and namespaces. We haven't talked about namespaces but we're going to do
03:03
that in the next video where we're going to create a real little project
03:07
together and this is now what our Compose and JSON field looks like but
03:11
we're going to take a look at this directly in an editor in a bit so let's
03:16
install our dependencies as well. Here we go and now we have our project open
03:22
up in PHPStorm which is a very famous editor for PHP. Of course there are a lot
03:26
of other editors and IDEs you can use. We will also link to a PHPStorm version
03:32
which is for you free to use but there's also VS Code and other ones out there
03:36
which are also free so just pick the one that you like the most. So now let's take
03:41
a look at what we created here. So this is directory of our project so what we
03:45
have here is the Composer JSON file and this is what we basically have set up
03:49
with Composer in it in our terminal and there again we have a name of the
03:54
project, description, our required dependencies, Guzzle in this case and for
03:59
our development PHP unit and we have also set up auto loading. So auto loading
04:04
again is something that's very important in PHP so that all the specific classes
04:09
which are sorted in different directories are auto loaded in the
04:13
correct way and Composer helps us really a lot. This was really a pain
04:17
before Composer but yeah it's way better now and it just tells that we have now a
04:22
new namespace for the source directory and this is basically inside here where
04:27
you would provide all your PHP files which you need for this application and
04:31
then again we have some more information about this project like the authors and
04:36
yeah that's basically it. As a good comparison let's also take a look at the
04:40
Lerva project. So I'm here inside a new Lerva project and as you can see there
04:45
is a lot going on and you're going to learn about all of that in our other
04:48
course about Lerva but yeah I want to go down and just show you that basically
04:53
this whole Lerva application is also just a Composer project here with a
04:57
Composer.json file and if we check this out you can see it basically looks
05:01
very similar to our simple project here with just some information about the
05:05
application. We also have your keywords, more about license, some more packages
05:10
which we need especially for development, a little bit more about auto-loading and
05:15
then we can also define scripts which you can run for your terminal which are
05:18
also pretty cool but yeah there's a lot more for you to discover on your PHP and
05:23
Lerva journey but yeah basically I just wanted to show you that even a Lerva
05:27
project at its bone is just a Composer project and very similar to the simple
05:33
one which we just created together and that's also all that I wanted to show
05:37
you today about package management in PHP. When you start working with Lerva
05:42
you will see that Composer is used extensively for managing packages and
05:46
auto-loading and it's just a fundamental tool that makes modern PHP development
05:51
possible. In the next video it's finally time for us to build our own first
05:56
little PHP application together.