Composer is a dependency management tool for PHP. You can specify which libraries (packages) your project depends on, and composer will install them for you. Composer is a dependency manager, not a package manager - this means that libraries (packages) are installed inside each project separately, and not globally into the system.

Why do you need Composer?

In the PHP world there is a tendency to reinvent wheels over and over again. This is due to the fact that for some time there was no convenient dependency manager, and no place where suitable solutions could be found. Installing third-party libraries often entailed the need to search for and install other libraries. As a result, sometimes it was easier to write your own library than to install and support someone else’s.
Composer takes the hassle out of dependency issues, and thanks to the official packagist.org repository, finding the package you need is super quick and easy.

How Composer works

The idea of ​​composer is not new; during its development, ideas were taken from the package manager for node.js - npm and Bundler - a manager for managing dependencies in ruby ​​applications.

  1. Your project depends on a number of libraries
  2. Some of these libraries depend on other libraries.
  3. You indicate which libraries you need directly
  4. Composer finds the necessary libraries of the versions you need and installs them (in the project folder), simultaneously installing the libraries necessary for the operation of these libraries.

Where to find packages

By default, packages are downloaded from the official packagist.org repository. Anyone can add a package there or download it.
You can also download packages directly from any git, svn or mercurial repository, or it can just be a zip archive available at any address.
The installed library does not have to be in the form of a Composer package.

Declaring dependencies

Let's say you're creating a project and you need a logging library. You decide to use monolog. All you need to add it to the project is composer.json a file that describes the project's dependencies.

("require": ("monolog/monolog": "1.2.*") )

We simply indicate that our project requires the installation of the monolog/monolog package of any version starting with 1.2 (for example 1.2.1, 1.2.2, etc.)

Read more about the capabilities and use of Composer in the following lessons.

Hi all. Today we will talk about what are package managers, and consider one of them - composer.

First, let's figure out what we need package managers? Package managers help you download all packages, dependencies, some frameworks, plugins used by the programming language through the console in just a couple of lines. In our case composer- This package manager for the php programming language.

To show you how composer works, let's download yii framework

So, go to the site http://getcomposer.org/ and press the button "Getting Started". Now click Installation - *nix to install it on Mac or Linux. Open a terminal and paste the following commands:

1) $ curl -sS https://getcomposer.org/installer | php

2) $ mv composer.phar /usr/local/bin/composer

Once you are done, enter the command composer and, if you have a large inscription "COMPOSER" and some information, then you did everything correctly and composer installed successfully.

to install composer on Windows, follow this link https://getcomposer.org/doc/00-intro.md#installation-windows and download the installer. If during installation you get library errors, just go to the file php.ini and disable those libraries that prevent installation package manager composer.

After composer installed, go to your desktop and create a folder called "composer". Now in the console go to it

Cd Desktop/composer/

To initialize composer, enter the command

Composer init

What's the point anyway? The point is that when you start a new project, you don’t need to climb websites and download everything you need for it. You simply enter the command in the console and everything is downloaded automatically. Another plus is that some libraries depend on other libraries, but you don’t need to worry about this anymore, because composer will download them too. In large projects this is very convenient in the sense that if, for example, a new employee arrives, you do not need to explain to him what to download and from where. You just give it to him json file, he will enter the command in the console and everything will be installed.

Let's continue installing our framework. As I already said, we enter

Search for a package:

Enter the name of our framework here

Search for a package: yii

You will see in front of you all the matches that you found. composer. Our need yiisoft/yii There is a number in square brackets on the left. In my case it's 0 , I enter it and press enter. Next we need to enter the version. Where does composer download all this from? There is a site where a lot of stuff is stored - http://packagist.org/ There enter in the search bar yii and follow the first link, there you will see that the version is called dev-master. Type this into the console and press enter.

Do you confine generation?

Above this caption you can see what the file looks like composer.json. This is exactly the file that you will give to a new employee.

So, we are happy with everything, press enter.

Now if you go to our folder on your desktop composer, then you will see that ours has appeared there json file.

Now enter the command into the console

Composer install

After the installation is complete, a new folder named vendor, where all the files of our framework are stored.

It's so easy to work with composer package manager, and most importantly, now you don’t have to download everything manually. It's enough to do it once json file and then simply use it to download and install the framework, plugins, libraries and other things you need.

Composer revolutionized package management in PHP and helped developers around the world create framework-independent and shared code. But still, few people go beyond the basics of its functionality, so this article will try to highlight some useful techniques for using it.

Global installation

Although this option is clearly described in the documentation, Composer can (and in most cases should) be installed globally. A global installation means that instead of:

Php composer.phar somecommand
In any project you can simply enter:

Composer somecommand
This makes it very easy to create new projects (for example, using the command create-project) anywhere on your file system.

To install Composer globally, follow these instructions.

Correct installation of dependencies

When reading introductory instructions or README files, many will write you something like:
Just add the following to your composer.json file:
("require": ("myproject": "someversion"))

But this approach has several disadvantages. Firstly, simple copy-paste can lead to errors. Secondly, it may not be obvious to a beginner where to place this code if he already has a large file composer.json, and this will also lead to an error. Finally, some people will be using Composer for the first time, and perhaps their first time using the command line. So it's a good practice to cover all sorts of cases in which newbies might feel unsure (do they have a graphical editor or will they use the command line? If the latter, does it have a text editor installed, and if so, which one? Do you explain yourself procedure for editing a file? What if the composer.json file does not yet exist in the project? Do you also describe how to create a new file?).

Best way to add a new dependency to a file composer.json- this is to use the command require:

Composer require somepackage/somepackage:someversion
This will add everything needed to the dependency file without manual intervention.

If you need to add packages to the section require-dev, add the option to the command --dev:

Composer require phpunit/phpunit --dev
Also, team require supports adding multiple packages at the same time, just separate them with a space.

Lock files

File composer.lock saves the current list of installed dependencies and their versions. This way, by the time the dependency versions are already updated, other people who clone your project will get the same versions. This ensures that everyone who receives your project has a “package environment” identical to the one you used during development, and helps avoid errors that might arise from version upgrades.

Also, file composer.lock contains the hash of the file composer.json, so if you even just update the project author details, you will get a warning that the lock file does not match .json file. In this case, the team will help composer update --lock, which will only update the lock file itself, without touching anything else.

Versioning

When specifying valid package versions, you can use exact match ( 1.2.3 ), ranges with comparison operators ( <1.2.3 ), combinations of these operators ( >1.2.3 <1.3 ), “last available” ( 1.2.* ), tilde character ( ~1.2.3 ) and caret ( ^1.2.3 ).

The last two instructions deserve a separate explanation:

  • tilde indication ( ~1.2.3 ) will include all versions up to 1.3 (not inclusive), since in semantic versioning this is the moment of introducing new functionality. In this case, the last of the stable minor versions will be obtained. As the documentation states, with this indication, the only last digit of the version.
  • specifying the caret ( ^1.2.3 ) literally means “beware of breaking changes only” and will include versions up to 2.0 . In relation to semver, changing the major version is the moment when breaking changes are made to the project, so the versions 1.3 , 1.4 And 1.9 fit while 2.0 - not anymore.
Unless you know you need a specific version of a package, I recommend always using the format ~1.2.3 is the safest choice.

Local and global configuration

The default parameter values ​​are not set in stone. Detailed description of possible configuration parameters ( config) see the link.

For example, by specifying:
("config": ("optimize-autoloader": true))
you force Composer to optimize the classmap after every package installation or update (or in other words, every time a class autoloading file is generated). This is a little slower than creating the default autoloader, and slows down as the project grows.

Another useful parameter could be cache-files-maxsize. In large projects (like eZ Publish or Symfony) the cache can fill up quite quickly. Increasing the cache size will allow Composer to run fast for longer.

Please note that configuration parameters can be set globally, in which case they will affect all projects (see config). For example, to globally set the cache size parameter, you either need to edit the file ~/.composer/config.json, or run:

Composer config --global cache-files-maxsize "2048MiB"

Profiling and verbose output (verbose)

If you add the parameter --profile to any command when using Composer on the command line, the output will contain more than just the final result, for example:

Memory usage: 174.58MB (peak: 513.47MB), time: 54.7s
But it will also add the command execution time and memory size used to the beginning of each line of output:

Installing assets for Sensio\Bundle\DistributionBundle into web/bundles/sensiodistribution
I use this option to identify "slow" packages and to monitor performance improvement or degradation on .

Similar to the previous one, the parameter --verbose will cause Composer to output more information about each operation being performed, letting you know that exactly happening at the moment. Some people even install composer --verbose --profile team alias composer default.

User sources

If your project is not yet on Packagist, sometimes you just need to install a package from GitHub (for example, if the package is still in development). To do this, see our guide.

When you have its version of a popular package that your project depends on, you can use custom origins in combination with inline aliasing to substitute your own branch for the public package, like Matthieu Napoli.

Speeding up Composer

Using a great method described by Mark Van Eijk, you can speed up Composer execution by calling it through HHVM.

Another way is using the parameter --prefer-dist, when installed, Composer will download stable, packaged versions of the project, instead of cloning from version control (which is much slower). This option is used by default, so you don't need to enable it on stable projects. If you need to download a project from sources, use the option --prefer-source. You can find out more about this in the section install .

Reducing Composer Project Size

If you are a developer of Composer-friendly projects, this part will also interest you. According to this post on Reddit, you can use the file .gitattributes ignore some files and folders when packaging a package for mode --prefer-dist.
/docs export-ignore /tests export-ignore /.gitattributes export-ignore /.gitignore export-ignore /.travis.yml export-ignore /phpunit.xml export-ignore
How it works? When you upload a project to GitHub, it automatically makes the link available “Download zip”, with which you can download the archive of your project. Moreover, Packagist uses these automatically generated archives to download dependencies with the option --prefer-dist, which it will then unzip locally (much faster than cloning the project's source files). If you add to .gitattributes tests, documentation and other files that are not related to the logic of the project, these archives will not contain them, becoming much lighter.

At the same time, people who want to debug your library or run tests, you will need to specify a parameter --prefer-source.

PhpLeague has adopted this approach and incorporated it into its "package skeleton"(Package skeleton), so any project based on it will automatically be “dist friendly”.

Show

If you suddenly forgot which version of PHP or its extensions you are using, or you need a list of all installed projects (with a description of each) with their versions, you can use the command show with parameters --platform (-p) And --installed (-i):

composer show --installed

$ composer show --installed behat/behat v3.0.15 Scenario-oriented BDD framework for PHP 5.3 behat/gherkin v4.3.0 Gherkin DSL parser for PHP 5.3 behat/mink v1.5.0 Web acceptance testing framework for PHP 5.3 behat/mink-browserkit -driver v1.1.0 Symfony2 BrowserKit driver for Mink framework behat/mink-extension v2.0.1 Mink extension for Behat behat/mink-goutte-driver v1.0.9 Goutte driver for Mink framework behat/mink-sahi-driver v1.1.0 Sahi. JS driver for Mink framework behat/mink-selenium2-driver v1.1.1 Selenium2 (WebDriver) driver for Mink framework behat/sahi-client dev-master ce7bfa7 Sahi.js client for PHP 5.3 behat/symfony2-extension v2.0.0 Symfony2 framework extension for Behat behat/transliterator v1.0.1 String transliterator components/bootstrap 3.3.2 The most popular front-end framework for developing responsive, mobile first projects on the web. components/jquery 2.1.3 jQuery JavaScript Library doctrine/annotations v1.2.4 Docblock Annotations Parser doctrine/cache v1.4.1 Caching library offering an object-oriented API for many cache backends doctrine/collections v1.3.0 Collections Abstraction library doctrine/common v2. 5.0 Common Library for Doctrine projects doctrine/dbal v2.5.1 Database Abstraction Layer doctrine/doctrine-bundle v1.4.0 Symfony DoctrineBundle doctrine/doctrine-cache-bundle v1.0.1 Symfony2 Bundle for Doctrine Cache doctrine/inflector v1.0.1 Common String Manipulations with regard to casing and singular/plural rules. doctrine/instantiator 1.0.4 A small, lightweight utility to instantiate objects in PHP without invoking their constructors doctrine/lexer v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers. egulias/listeners-debug-command-bundle 1.9.1 Symfony 2 console command to debug listeners ezsystems/behatbundle dev-master bd95e1b Behat bundle for help testing eZ Bundles and projects ezsystems/comments-bundle dev-master 8f95bc7 Commenting system for eZ Publish ezsystems /demobundle dev-master c13fb0b Demo bundle for eZ Publish Platform ezsystems/demobundle-data v0.1.0 Data for ezsystems/demobundle ezsystems/ezpublish-kernel dev-master 3d6e48d eZ Publish API and kernel. This is the heart of eZ Publish 5. ezsystems/platform-ui-assets-bundle v0.5.0 External asset dependencies for PlatformUIBundle ezsystems/platform-ui-bundle dev-master 4d0442d eZ Platform UI Bundle ezsystems/privacy-cookie-bundle v0. 1 Privacy cookie banner integration bundle into eZ Publish/eZ Platform fabpot/goutte v1. 0.7 A simple PHP Web Scraper friendsofsymfony/http-cache 1.3.1 Tools to manage cache invalidation friendsofsymfony/http-cache-bundle 1.2.1 Set path based HTTP cache headers and send invalidation requests to your HTTP cache guzzle/guzzle v3.9.3 PHP HTTP client. This library is deprecated in favor of https://packagist.org/packages/guzzlehttp/guzzle hautelook/templated-uri-bundle 2.0.0 Symfony2 Bundle that provides a RFC-6570 compatible router and URL Generator. hautelook/templated-uri-router 2.0.1 Symfony2 RFC-6570 compatible router and URL Generator imagine/imagine 0.6.2 Image processing for PHP 5.3 incenteev/composer-parameter-handler v2.1.0 Composer script handling your ignored parameter file instaclick/php -webdriver 1.0.17 PHP WebDriver for Selenium 2 jdorn/sql-formatter v1.2.17 a PHP SQL highlighting library knplabs/knp-menu v1.1.2 An object oriented menu library knplabs/knp-menu-bundle v1.1.2 This bundle provides an integration of the KnpMenu library kriswallsmith/assetic v1.2.1 Asset Management for PHP kriswallsmith/buzz v0.13 Lightweight HTTP client league/flysystem 0.5.12 Many filesystems, one API. liip/imagine-bundle 1.2.6 This Bundle assists in imagine manipulation using the imagine library monolog/monolog 1.13.1 Sends your logs to files, sockets, inboxes, databases and various web services nelmio/cors-bundle 1.3.3 Adds CORS ( Cross-Origin Resource Sharing) headers support in your Symfony2 application ocramius/proxy-manager 0.5.2 A library providing utilities to generate, instantiate and generally operate with Object Proxies oneup/flysystem-bundle v0.4.2 Integrates Flysystem filesystem abstraction library to your Symfony2 project. pagerfanta/pagerfanta v1.0.3 Pagination for PHP 5.3 phpdocumentor/reflection-docblock 2.0.4 phpspec/prophecy v1.4.1 Highly opinionated mocking framework for PHP 5.3+ phpunit/php-code-coverage 2.0.16 Library that provides collection, processing, and rendering functionality for PHP code coverage information. phpunit/php-file-iterator 1.4.0 FilterIterator implementation that filters files based on a list of suffixes. phpunit/php-text-template 1.2.0 Simple template engine. phpunit/php-timer 1.0.5 Utility class for timing phpunit/php-token-stream 1.4.1 Wrapper around PHP"s tokenizer extension. phpunit/phpunit 4.6.4 The PHP Unit Testing framework. phpunit/phpunit-mock-objects 2.3 .1 Mock Object library for PHPUnit psr/log 1.0.0 Common interface for logging libraries qafoo/rmf 1.0.0 Very simple VC framework which makes it easy to build HTTP applications / REST webservices sebastian/comparator 1. 1.1 Provides the functionality to compare PHP values ​​for equality sebastian/diff 1.3.0 Diff implementation sebastian/environment 1.2.2 Provides functionality to handle HHVM/PHP environments sebastian/exporter 1.2.0 Provides the functionality to export PHP variables for visualization sebastian/global -state 1.0.0 Snapshotting of global state sebastian/recursion-context 1.0.0 Provides functionality to recursively process PHP variables sebastian/version 1.0.5 Library that helps with managing the version number of Git-hosted PHP projects sensio/distribution-bundle v3 .0.21 Base bundle for Symfony Distributions sensio/framework-extra-bundle v3.0.7 This bundle provides a way to configure your controllers with annotations sensio/generator-bundle v2.5.3 This bundle generates code for you sensiolabs/security-checker v2.0.2 A security checker for your composer.lock swiftmailer/swiftmailer v5.4.0 Swiftmailer, free feature-rich PHP mailer symfony-cmf/routing 1.3.0 Extends the Symfony2 routing component for dynamic routes and chaining several routers symfony/assetic-bundle v2.6.1 Integrates Assetic into Symfony2 symfony/monolog-bundle v2.7.1 Symfony MonologBundle symfony/swiftmailer-bundle v2.3.8 Symfony SwiftmailerBundle symfony/symfony v2.6.6 The Symfony PHP framework tedivm/stash v0.12.3 The place to keep your cache. tedivm/stash-bundle v0.4.2 Incorporates the Stash caching library into Symfony. twig/extensions v1.2.0 Common additional features for Twig that do not directly belong in core twig/twig v1.18.1 Twig, the flexible, fast, and secure template language for PHP white-october/pagerfanta-bundle v1.0.2 Bundle to use Pagerfanta with Symfony2 whiteoctober/breadcrumbs-bundle 1.0.2 A small breadcrumbs bundle for Symfony2 zendframework/zend-code 2.2.10 provides facilities to generate arbitrary code using an object oriented interface zendframework/zend-eventmanager 2.2.10 zendframework/zend-stdlib 2.2 .10 zetacomponents/base 1.9 The Base package provides the basic infrastructure that all packages rely on. Therefore every component relies on this package. zetacomponents/feed 1.4 This component handles parsing and creating RSS1, RSS2 and ATOM feeds, with support for different feed modules (dc, content, creativeCommons, geo, iTunes). zetacomponents/mail 1.8.1 The component allows you construct and/or parse Mail messages conforming to the mail standard. It has support for attachments, multipart messages and HTML mail. It also interfaces with SMTP to send mail or IMAP, P. .. zetacomponents/system-information 1.1 Provides access to common system variables, such as CPU type and speed, and the available amount of memory.

Rehearsals (Dry Runs)

To simply see if installing new dependencies will succeed, you can use the option --dry-run for teams install And update. Composer in this case will display all potential problems without directly executing the command itself. There will be no real changes to the project. This technique is great for testing complex dependencies and tweaking changes before actually making them.

Composer update --dry-run --profile --verbose

Creating a Project

The last but not least thing we must mention is the team create-project.

The create project command takes as an argument the name of the package, which it then clones and executes composer install inside it. This is great for initializing projects - no more searching for the Url of the desired package on GitHub, cloning it, going to the folder yourself and executing the command install.

Large projects such as Symfony and Laravel are already using this approach to initialize their "skeleton" applications, and many others are joining in as well.

For example, in Laravel it is used like this:

Composer create-project laravel/laravel --prefer-dist --profile --verbose
To the team create-project You can pass two more parameters: path, into which the project should be installed (if not specified, the package name is used), and version(the latter will be used if not specified).

Conclusion

I hope you found this list of tips and tricks helpful. If we missed something, please tell us about it and we will update the article. And remember, if you forgot any commands or options, just check out the cheat sheet. Happy Composing!

From the author: Very often, when developing web applications, especially large web projects, it is necessary to use various third-party libraries. For example, it could be a PHP framework, or a template engine, or a forum engine, or all of these components together. In this lesson we will look at the Composer dependency manager, with which you can easily download and install the necessary libraries

If we use several third-party libraries, all of them must be installed correctly and connected to the script being developed. In the best case, each library needs to include one main file, while if we use three third-party libraries, then it is necessary to include three files.

But what if one of the libraries depends for its functionality on additional libraries, in which case they also need to be downloaded and connected. That is, when using several libraries, there are some inconveniences associated with their installation.

Therefore, in this lesson we will look at the Composer dependency manager, with which you can easily download and install the necessary libraries.

Installing Composer

Composer is a dependency manager for the PHP language interpreter; to put it simply, it is a script written in PHP that downloads the libraries you need and automatically generates a single special file, by connecting which you will include all the downloaded libraries. Moreover, if the libraries you need depend on some additional libraries, they will also be downloaded automatically. Libraries are downloaded from the official package repository packagist.org.

As I said earlier, Composer is a dependency manager for the PHP language interpreter, which means this tool is installed directly into the interpreter of this language. At the same time, now we are talking about the interpreter that is installed on your home computer, because often on the server (on a real hosting on the Internet) we do not have access to the PHP language interpreter. And this is not necessary at all, since scripts are mainly developed on a home computer and transferred to hosting upon completion of work. Of course, the PHP language interpreter can be installed differently for everyone, for example, someone uses Denwer software, someone OpenServer, someone with a separate installation of PHP, Apache, Mysql, but this is not at all important since the installation process of the Composer dependency manager is similar for everyone cases.

Before installation, let's take a look at the official website of the Composer dependency manager - https://getcomposer.org/:

Here on the Documentation page there is a detailed description of how to install and work with this manager (though in English).

Composer can be installed on the Windows operating system in two ways:

manually using the command line;

automatically, using a special file, the link to which you will find on the documentation page, in the installation section for Windows OS.

In this lesson we will look at the manual method of installing the Composer tool. I would immediately like to note that Composer is a composer.phar file, which is usually located in the folder with the PHP language interpreter. Therefore, before installation, it is advisable to view this folder, because, for example, in the OpenServer software (in PHP modules), Composer is already installed.

So, having started the web server, open the command line (let me remind you that for Windows 7 the command line can be opened if you enter cmd in the Start menu search), and go to the folder in which the PHP language interpreter is installed. To do this, use the cd command: cd folder path

Now according to the documentation, you need to run the following command: php -r “readfile(‘https://getcomposer.org/installer’);” | php

Which will execute the PHP code readfile(‘https://getcomposer.org/installer’), that is, we will read the remote file. Here I would like to note that in your PHP interpreter, the php_openssl.dll extension must be enabled, otherwise the command will not be executed.

After executing the command, we see a message stating that the installation was completed successfully. You can check whether Composer was actually installed using the command, which will show its version: php composer.phar -v

Now, for the convenience of working with it, let's run one more command: echo @php "%~dp0composer.phar" %*>composer.bat

This command will create a special file composer.bat in the PHP language interpreter folder, with which you can access the dependency manager using only the name composer, and at the same time, being in any folder from the command line. But at the same time, in the system variable path, you need to specify the path to the folder in which the PHP language interpreter is installed:

Installing the required libraries

First, let's agree that our script will require the following libraries:

Now, you need to describe in a special file composer.json (we create this file in the folder with the script being developed) that the above libraries are necessary for the future script to work:

( "require": ( "slim/slim":"2.*", "twig/twig":"~1.0", "phpbb/phpbb": "3.1.3-RC2" ) )

"require" :(

"slim/slim" : "2.*" ,

"twig/twig" : "~1.0" ,

"phpbb/phpbb" : "3.1.3-RC2"

As you can see, this file must contain an object in the form of a json string. Which, in its require property, describes those libraries on which the future script depends. Moreover, require is, in turn, an object whose properties are the libraries that need to be downloaded. Where the property name is the name of the library, and the value is the version of the library being downloaded. Moreover, the name consists of two substrings separated by /. The line before the separator is the name of the vendor, the line after is the name of the library.

The names and versions of the libraries that must be specified in the composer.json file are listed on the official websites, in the installation section. For example, for the Twig template engine, in the documentation, in the Installation section, there is a line that I entered into the composer.json file:

After compiling the composer.json file, open the command line, go to the folder of the script being developed, and execute the command: composer install

And the installation of the necessary libraries starts immediately.

After installation, in the folder of the script being developed, we will find the vendor folder, into which all the necessary libraries and their dependencies were downloaded, and the autoload.php file was generated, by connecting which you will connect all the downloaded libraries. After this, you can work with the installed libraries.

This concludes this lesson. All the best to you and happy coding!

What is this and why do I need it?

If you have this question, then do not read further from the article and first ask google “what is dependency injection?” and why do you need dependency manager(in particular ).

Personally, I no longer have applications without composer. Especially when developing for a framework. Here are just the fundamental benefits of using a dependency manager in projects:

  • Convenient installation and updating of third-party packages and libraries
  • Correct deployment
  • Someone else's code from third-party packages is not committed to its repository, and does not litter the code and statistics
  • Flexible dependency versioning

Installation

Everything here is as simple as possible, but first you need to decide on the method of use: one composer for the entire localhost or each project has its own composer? (you can of course combine)

The first option is convenient for developers who simultaneously work on many applications and keep everything clean and fresh. The second option is used when the application is required to have an accurate and uniform version of the composer for all developers, and then it is committed to the repository.

Keeping Composer together with the application

Composer Commands

install

Having installed and collecting your composer.json you can start installing the very packages that we specified in our json file in the “require” and “require-dev” blocks. For this we use the command install.

Composer install ./composer.phar install php composer.phar install

* The first command (line 1) for those who chose the single installation method for the entire operating system. The second and third are needed by everyone else and differ only in whether you have given the rights to run the phar file or not.

When you first run the command install we will receive the latest or exact version of all packages that we specified in the json file (depending on how we specified the version for each individual package). Rerunning the command install will lead to nothing - and this is an important point!

During execution install downloads all libraries to a folder vendor of our project (below I will describe how to change this path), and next to json-a new file will be created with the file composer.lock(this is a snapshot - a snapshot of installed packages indicating their exact versions). Folder vendor should be immediately placed in .gitignore, so as not to accidentally mix everything with your code. And here lock- the file, on the contrary, needs to be committed and sent to the repository. If you delete a folder vendor or some packages from it and then call the command again install, then all packages will be restored with exactly the same versions that were installed when first called install. This happens because checks availability lock-file, and if present, ignores the package versions specified in the json file (only if they were not lowered!), and installs the versions specified in lock-file.

Thus, passing on a single lock-file between all developers, the team will be guaranteed to have the same package versions (I don’t think it’s worth explaining why).

update

If it is necessary to update the package versions to the latest ones, call the command update.

Composer update ./composer.phar update php composer.phar update

After which all packages will be updated. It will also be updated lock-file.

It’s worth remembering that packages will be updated in accordance with the version format specified for each of them separately in json-file. The syntax for specifying versions for packages is well described in the official documentation.

self-update

Another useful command that will update itself .

Composer self-update ./composer.phar self-update php composer.phar self-update

show

./composer.phar show -s -t ./composer.phar show -i -t

This command displays a list of installed packages.

First example (with key -s) will display a tree of packages that we ourselves manually specified in json-file.

Second example (with the key -i) will display absolutely all installed packages, recursively including the dependencies of all our original packages.

Key -t changes the display of the response to a dependency tree style.

vendor path or how to change the installation path of libraries

To change the installation path of packages, you must use the directive vendor-dir V json-file. Here's an example:

( "name": "My Project", "description": "Project description", "homepage": "http://my-project.com/", "config": ( "vendor-dir": "/vendor /custom/path" ), "require": ( "php": ">=5.3.3", "zendframework/zendframework": "2.*" ), "require-dev": ( "zendframework/zftool": "v0.1.0", "zendframework/zend-developer-tools": "dev-master" ) )

Conclusion

I tried to describe everything basic that might be needed to get acquainted with Composer. It is better to study deeper features from the documentation. For example: how to enable downloading of your own library from github.

And to easily find the necessary packages, I advise you to use the packagist.org project

If you have any questions, feel free to write in the comments.