Composer Introduction

Composer Tutorial

In this article, we’ll see Composer Introduction.

The very first thing you will need to work with Laravel (and then Eloquent) is C0mposer. It is a dependency management tool for PHP. With this tool, you can easily include every dependency that is needed in your project. This is done in seconds, using a JSON configuration file namedc0mposer.json

What is Composer

It is a dependency manager for PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

It is a popular tool among PHP developers, and it is used by many popular projects, such as Laravel and Symfony.

It is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. It is a dependency manager

  1. It enables you to declare the libraries you depend on.
  2. Finds out which versions of which packages can and need to be installed, and installs them (meaning it downloads them into your project).

Usually, dependencies in a PHP project were managed with PEAR or other methods. It has a different policy: everything works on a per-project basis. This means that you can have two projects on the same server with different versions of the same dependency package.

Installation

The installation procedure is ridiculously easy. All you have to do is to go on the Download page of the Composer website and find the right method for your operating system.

If you have Linux or Mac, just use this:

curl -sS https://getc0mposer.org/installer | php

Or, if you don’t have cURL, then use this:

php -r "readfile('https://getcomposer.org/installer');" | php

Also, the composer.phar the file will be downloaded in your current directory.

On Windows, you can simply download the dedicated installer.

Once It is installed, I suggest putting its path in the PATH variable of your system, in order to use it wherever you want. There are many ways to do it, which depend on your operating system. Let’s look at each.

On Linux, you can move It to the right directory simply with the following command:

mv composer.phar /usr/local/bin/composer

The same goes for OS X, but sometimes, the usr directory doesn’t exist. You must createusr/local/bin manually.

Finally, on Windows, you must open the control panel and type environment variable or something similar. The search utility will do the rest for you. Once in the right window, you will get a list of all environment variables. Find PATH and add the composer installation path to it.

The composer.json and autoload files

Before we go deep into our project, let’s take a look at how it works.

In the composer.json file, the developer specifies every single dependency for its project. You can also create your packages, but we are not going to look at how to create them in this book.

So, let’s say that you want to create a project that uses Monolog for logging purposes.

Create a folder for the project, then create an empty text file, and name it composer.json.

Open it and all you will have to do is to include your dependency as shown:

  {
      "require": {
          "monolog/monolog": "1.12.0"
      }
  }

After that, save the file and type the following in your project directory:

composer update

Wait a minute to download everything, and then you are done!

What? OK, here is how it works: It downloads every package you may need and automatically creates a loader for all your packages. So, to use your dependencies in your project, you will just need to include vendor/autoload.php, and you are good to go.

Let’s say that you have a index.php file as a start file for your application. You will have to perform something like the following:

  <?php // index.php file

    require('vendor/autoload.php');

    // your code here...

Nothing more!

Why am I showing this to you? Well, Laravel and Eloquent are Composer packages. So, in order to use it and create a Laravel application, you have to know how the mechanism works!

The most used commands

It is a command-line tool. Every good CLI tool has some important commands, and in this little section, I will show you what we are going to use the most.

First of all, we have the following:

composer create-project

With this command, you can create a new project using a specific package as a base. You will use this command to create a new Laravel project using the following syntax:

composer create-project laravel/laravel my_project_folder

Then, you can find:

composer install
composer update

These are two similar commands; they are similar, but not the same. When you specify your dependencies in the composer.json file, you can use install to install them. If you already installed them but you want to update your dependencies to a newer version, use update.

Note

In order to know what must and must not be updated, It uses thecomposer.lock file, which you can see at the root of your project. Actually, you will never have to work with it, but it’s important to know that it uses it as a log of what it does.

Sometimes, you will also see this:

composer require

You can use require to include dependencies in your project on the fly. Here’s an example of Monolog inclusion using require:

composer.phar require monolog/monolog:1.12.0

Another often-used command is:

composer dump-autoload

This command regenerates the autoload.php file. It can be useful if you add some classes into your projects without using namespaces or PSR conventions and rules.

Sometimes, you will have to use (after a warning):

composer self-update

This command updates the C0mposer itself. Just a few seconds and you are up and running again!

Finally, you can use the following special command:

composer global COMMAND_HERE

Use it to execute a specific command in the C0mposer home directory. As I mentioned before, It works on a per-project basis, but sometimes you will need to install some tools globally. With the global command, you can do it easily.

I hope this article helps!

Write a Reply or Comment

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