Composer and its uses within Zend Framework 2
Composer is a dependency manager tool for PHP, which has been live since the spring of 2011, and is incredibly handy when it comes to getting projects set up with ease.
Composer reads its configuration from a file called composer.json
, which is a JSON file that is being read by composer.phar
(PHP archive).
We can use Composer to initialize the Zend Framework 2 library when we are using the Zend Framework 2 skeleton application. Other functionalities within Zend Framework 2 include installing new modules or libraries, which we can use to extend our application.
The composer.json file
If we open up the composer.json
file we can see that the file has a couple of keys defined, which tells the Composer what it needs to load, and what versions we need. By default, the Zend Framework 2 skeleton application's composer.json
will look similar to the following:
{ "name": "zendframework/skeleton-application", "description": "Skeleton Application for ZF2", "license": "BSD-3-Clause", "keywords": [ "framework", "zf2" ], "homepage": "http://framework.zend.com/", "require": { "php": ">=5.3.3", "zendframework/zendframework": ">2.2.0rc1", } }
As we can see the file is pretty easy to understand, and the keys are pretty self explanatory, but to be sure we will go through them quickly to make sure we understand what is going on.
name
: This is the name of the package with the vendor name as the prefix, in this case the vendor iszendframework
and theskeleton-application
is the package.description
: This short description tells us what the package does.license
: This is the license the software is licensed under, normally this is one of the numerous open source/software licenses such as the BSD, GPL and MIT licenses. However, a closed-source software license is also available under the key 'proprietary'.keywords
: This is an array of keywords that is used when searching for this package on the getcomposer.org website.homepage
: Well this is pretty clear, is it not?require:
Now this is getting interesting, as this will tell Composer exactly what we need to run our package. In this case it is an array with PHP, where we need Version 5.3.3 or higher and Zend Framework 2 version 2.2.0rc1 or higher. Please note however, that in production we should always avoid a dev Version or a package with a greater than symbol, as it could potentially break our application. Always (please remember!) to get the exact version required when putting the application live.
Although it doesn't say it here, Composer will always install Zend Framework 2 to the vendor directory, as the required section in the composer.json
says we need zendframework/zendframework
to run our application. Composer knows that it needs to be installed to the vendor directory because the zendframework/zendframework
package is of the type library, and that type is always being copied by Composer to the vendor directory.
Upgrading packages
Sometimes we just want to update our libraries, for example, when we know that a bug has been solved in Zend Framework 2's library, and we really want to have it. Fortunately, Composer comes with a great self-update and update command that we have for our disposal.
To update our libraries automatically through Composer, we should execute the following commands in the terminal (this cannot be done properly through the web browser):
$ php composer.phar self-update
First we want to make sure that we are using the latest Composer, as using an outdated Composer might give unnecessary errors.
$ php composer.phar update
This will update all our packages that we have put in the require
section of the composer.json
to update to the latest (compatible) version. We should be wary, however, that when we want a new package installed, but without the updation rest of the packages, we should use the following command:
$ php composer.phar update vendor-name/package-name
Here vendor-name
and package-name
are the names of the packages we want to install.
Composer works because all the packages are registered on their website getcomposer.org. In the website they keep all the packages together, and whenever we try to update or install, the composer.phar
will connect to the website and retrieve the newest packages.
When we create our own modules or libraries, we can also submit that to the composer website. Submitting to composer's website will create a better community and a better understanding of the dependencies needed when we begin developing certain applications.
See also
The composer's main website http://getcomposer.org