قالب وردپرس درنا توس
Home / Tips and Tricks / How to install and use Composer, PHP’s Dependency Manager – CloudSavvy IT

How to install and use Composer, PHP’s Dependency Manager – CloudSavvy IT



PHP logo

Composer is the dependency manager of the PHP community. It simplifies installing, updating and using third-party packages. Packages can be hosted by public and private repositories, with most popular projects published to Packagist.

Install Composer

Composer is a community effort not bundled with PHP. It is distributed as a PHP PHAR archive from getcomposer.org. Some Linux distributions include Composer in their software repositories, but installing this way usually returns an outdated version.

Make sure you have installed PHP before proceeding. PHP 5.3 is the oldest supported version at the time of writing. You also need git and unzip on your system if you want to install packages from source.

Composer provides an automated installation script. Start by downloading the installer to your working directory:

curl https://getcomposer.org/installer -o composer-setup.php

You should now verify the installer hash to make sure it hasn̵

7;t been tampered with. To check the latest hash and get a sample verification code, check the Composer website.

Then use the installation script to install Composer:

php composer-setup.php --install-dir=/usr/local/bin --filename=composer

This will download Composer to /usr/local/bin, which can keep it in your path alongside your other executables. Try to run composer in your shell to make sure everything is working. The Composer version should be displayed, followed by a list of the available Composer commands.

Screenshot of Composer help output

To update Composer in the future, run composer self-update. This will automatically replace your Composer binary with the latest release. You do not need the composer-setup.php script again so you can delete it now.

Prepare your project

You must have one composer.json file in your project workbook before you can start using Composer. Run composer init to make one interactive. This will give you a basic configuration.

Screenshot of the Composer init command

Follow the command line prompts to enter information about your project, such as name, description, and author information. Package names use the vendor/package syntax to avoid conflicts between authors in public repositories. You must use your Packagist username if the vendor component.

Most keys in composer.json are optional unless you plan to publish your codebase to Packagist. You can find a full description of the file structure on the Composer documentation site.

Install packages

You can add packages to your project with the composer require order:

composer require vendor/package

Find packages to install using the Packagist website. They are added to it require section of your project composer.json File. Once installed, the package source code goes to the vendor folder within your project.

Screenshot of a Composer.json file

Composer relies on semantic versioning to handle package updates. The exact version of each package you have installed is written composer.lock in the folder of your project. This allows Composer to identify the specific package to install when composer.json indicates that a range of versions is acceptable.

You should commit both composer.json and composer.lock to your resource manager. Other developers working on your project can then be executed composer install to acquire all the dependencies you defined.

Packages such as test runners can be marked as development dependencies by the --dev flag to the require order. They are separated into one require-dev section inside composer.json. Use when installing your packages composer install --no-dev to rule out developmental dependencies. This is useful when using Composer within deployment scripts and CI systems.

Update packages

You should try to keep your packages up to date so that you don’t miss out on security and bug fixes. Carried out composer outdated command to see a list of dependencies in `composer.json` for which new versions are available.

Run to apply the updates composer update. This respects semantic versioning and pulls out the latest version of each package, within the version restrictions specified by your composer.json. A package marked as ^1.2 will be updated to 1.2.x or 1.3.x but not to 2.0. The Composer documents provide detailed information on how the tool resolves various forms of version limitation.

When you update a package, your composer.lock file to specify the new version. Other developers working on your project can restart composer install to obtain the exact packages you are using.

The Composer Autoloader

Autoloading is the mechanism of choice for discovering source files in PHP. Composer has premium support for autoloading; usually the autoloader is the only file you need require_once() within your project.

As you install dependencies, Composer automatically writes an autoloader to vendor/autoload.php. Packages specify how to automatically load them with the autoload field composer.json. You should set this up for your own project so that Composer can automatically load its files as well:

{
    "autoload": {
        "psr-4": {
            "ExampleProject\": "src/"
        }
    }
}

The above snippet configures the automatic loading of your project using the PSR-4 standard. Codebase resources within the ExampleProject namespace is mapped to files in the src directory – for example use ExampleProjectExampleClassesMyClass automatically require_once("src/ExampleProject/ExampleClasses/MyClass.php").

The only file you need manually require_once() is the autoloader itself:

require_once(__DIR__ . "/vendor/autoload.php");

You must add the line in your application as early as possible. This ensures that autoloading is enabled before you start using classes and interfaces within your codebase.

Sometimes you need to force autoloader regeneration. This is often because you have the autoload configuration. You can walk composer dump-autoload to write a new autoloader on demand.

Overview

Composer simplifies PHP development by providing the dependency manager that is missing from the core language. Composer makes it easy to include third-party code in your projects, without having to manually download source files and keep them up-to-date.

Composer’s built-in autoloader allows you to access installed packages without additional work on your part. The tool also includes a script runner that allows you to perform tasks within your codebase by adding commands to the scripts block in composer.json. Use composer run my-script to run the script.


Source link