Composer: a batch manager for PHP

Composer: a batch manager for PHP

The package manager is a program for finding and installing libraries by their name.

The package manager eliminates the need to independently search for libraries, download, unpack, resolve dependencies - he takes on all these tasks.

What does a package manager do?

  • Find libraries by name, download and unpack them;
  • Automatically download and install dependencies for each library ;
  • Connect library classes inside scripts;
  • Update libraries along with dependencies.

Composer: a batch manager for PHP

PHP uses a package manager called Composer to manage libraries. This is a powerful and convenient tool that will allow you to forget about the headache associated with finding, installing and resolving library dependencies forever.

How to get started with Composer

  • Download Composer;
  • Initialize it in the project;
  • Connect the startup file to the required script;
  • Install the required library.

Installation

Download Composer for Windows
. This is a regular setup file with a "wizard" mode that guides you through the entire installation process. At the end, you can test its work by opening a command line. If you execute the command composer, you will see a long list of its capabilities.

Initialization in the project

Let's continue working with the command line. First, let's go to the working folder of the project (if you installed OpenServer in the standard folder, then, for example, like this:) cd C:\ospanel\domains\localhost. Now let's execute the commands composer initand sequentially composer install. This completes the initialization. You may notice that a new folder has appeared in the project named vendor.

Connecting an autoload script

Composer makes it easy not only to install libraries, but also to use them. It takes care of including all the necessary library class files. A special script is responsible for this autoload.php.
The autoload.php script is the only file that needs to be included to use any libraries.

autoload.php uses an "autoload" mechanism. It intercepts calls to library classes and includes all the necessary scripts on the fly. For this to work, include autoload.php in your script:

require_once "vendor/autoload.php";

Installing the library from Composer

Composer downloads and installs libraries by name. This means that first you need to "google" the desired library, go to its website, and find its name there in the description. For example, the name of a library might be:fzaninotto/faker

Now we can ask composer to install the library. To do this, enter the command composer require . Composer will download and install the library to a folder vendor. All that remains is to connect the installed library in the script and you can use it.

Connecting a library in scripts

Let's consider connections and use on the example of a library for form validation - GUMP. Install it with: composer require wixel/gump.
Now let's include the library in the script where the form validation takes place:

<?php
require 'vendor/autoload.php';

$rules = [
    'email' => 'required|valid_email',
    'password' => 'required|min_len,8',
    'login' => 'required|alpha_numeric',
    'phone' => 'phone_number'
];

$gump = new GUMP('en');
$gump->validation_rules($rules);
$validated_data = $gump->run($_POST);

First, we connect universal startup file which is responsible for the connection of class libraries: vendor/autoload.php.

Then we create a new validator object and call its methods to pass the validation and form validation rules. That's all.