Jump Start Your Web Application With PHP Composer

If you are a web developer who loves PHP, I bet you have heard about Composer. Composer is a dependency manager for PHP. So what does dependency mean? In developing a web application you may need to use one or more libraries. In my case I often use these libraries: monolog, slim, Laravel, and a debug helper. Without Composer it would take me hours just to set up the environment. With Composer I just need to define a composer.json file, and Composer will handle the rest — downloading and compiling all libraries and creating the autoload. It is so convenient.

Install Composer on Windows

To install Composer on Windows is very easy, because Composer comes with an installer. You can download the installer from the Composer download page. After installation you can use Composer from your command prompt or, if you have Git Bash (msysgit) installed, you can use it from there as well. If you have trouble using Composer from the command line, you may need to check your path variable in your environment variables.

Basic Use of Composer

To get all the list of commands available you can just type composer list. To use Composer for your project you must define a composer.json file, where you can put the information of your project and the required libraries. See the example below for the basic use of a composer.json file:

{
"require": {
"monolog/monolog": "dev-master"
}
}

And to run the installation, you only need to type composer install, and all the required libraries will be downloaded and an autoload file will be created. Composer will place all the libraries into the vendor folder and then create the autoload file.

You can also use composer init to generate a more complex composer.json file — there will be an interactive prompt and it is easy to use. Here is one example:

{
 "name": "calibrefx/api",
 "description": "CalibreFx API System.",
 "keywords": ["api", "calibrefx"],
 "require": {
 "laravel/framework": "4.0.6",
 "way/generators": "dev-master",
 "zizaco/confide": "dev-master",
 "zizaco/entrust": "dev-master"
 },
 "autoload": {
 "classmap": [
 "app/commands",
 "app/controllers",
 "app/models",
 "app/database/migrations",
 "app/database/seeds",
 "app/tests/TestCase.php"
 ]
 },
 "scripts": {
 "post-install-cmd": [
 "php artisan optimize"
 ],
 "pre-update-cmd": [
 "php artisan clear-compiled"
 ],
 "post-update-cmd": [
 "php artisan optimize"
 ],
 "post-create-project-cmd": [
 "php artisan key:generate"
 ]
 },
 "config": {
 "preferred-install": "dist"
 },
 "minimum-stability": "dev"
}

The above code is for a Laravel project, and it is all auto-generated. All I need is just to execute this command:

composer create-project laravel/laravel your-project-name --prefer-dist

Then Composer will automatically set up everything for me, saving a lot of my time.

Calling the autoload file in PHP

After Composer downloads and creates the autoload file, you just need to call it once in your PHP code. See the example below:

require 'vendor/autoload.php';

The above code will call the autoload.php file generated by Composer, from which you can access all the classes and functions in your libraries.

And the great thing about Composer is that there are a lot of libraries available online — you can search them at packagist.org.