Composer part-2: Dependacy manager for PHP

In Previous post we have seen how we can autoload PHP classes and files by defining it in composer.json file and assign rest of the responsibility to composer to generate necessary files so that they are available to autoload in your PHP app file by just including vendor/autoload.php. Composer is not only limited to this, actually composer is created to manage dependencies in PHP applications.
Normally in PHP application if we require some third party library we are adding it in separate folder and include it in our PHP file. Composer is also doing the same thing while adding that dependency to a project but it manage it properly.

Fist of all composer requires composer.json which looks like as below let me show my previous post composer.json.

{
     "name": "My App",
     "description": "Core PHP App",
     "autoload": {
          "files":[
               "lib/files/lib_functions.php",
          ],
          "classmap": [
              "lib/classes/module1",
              "lib/classes/module2"
          ]
     }
}

Composer manages all the dependencies through https://packagist.org/. Let i want to install twig template engine (http://twig.sensiolabs.org/) in my app. So for that i have to update my composer.json file as below.

{
     "name": "My App",
     "description": "Core PHP App",
     "require": {
        "twig/twig": "1.*"
     },
     "autoload": {
          "files":[
               "lib/files/lib_functions.php",
          ],
          "classmap": [
              "lib/classes/module1",
              "lib/classes/module2"
          ]
     }
}

Notice the require block in above code. It indicates the dependencies handle by composer. You will have to first search on https://packagist.org/ and than update the composer.json accordingly so you will always get the package you want for your application. In above example “twig/twig”:”1.“,here 1. indicates version. Composer will install latest version available in 1.* series.

Once you are done with adding all the dependencies in composer.json require block just type below command from command prompt from the directory where your composer.json is located.

composer install

composer dump-autoload.

by executing above command you will notice vendor directory in you project and composer will install all the packages inside it.
If you want to update any pre-installed packages you have execute below command:

composer update

composer dump-autoload

Once pakage is insatlled you can use it by reading it’s documentation because it is not worth to explain use of each package available at https://packagist.org/.

I hope you have now some understanding the role of composer in modern way of developing PHP applications.

Thanks,
-Ahesan

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s