Why you should use Composer and how to start using it

Hello!

One of the most big changes happening in PHP world is the Composer. I’m shure you heard about it but maybe you don’t know why you should use it and how much it is good for you and your projects.

What is Composer?

When you need some specific code in PHP you can go to PHP Classes and search for what you need. For example, you need a class to connect to your Twitter account and get the last updates in your timeline. Ok, you’ll find it easy. And what you have to do after? You download the classe and copy to some place inside your project, and call it including or requiring it inside your PHP code, right?

Well, you can use Pear to. You must install it on your server and install some components that you can use like a package manager. This work too but nothing is installed inside your project structure, but in your machine.

Ok. Composer came to solve all this questions. Together with Composer we have some rules, making all PHP project with the same structure, using namespaces, the same code style, etc. This simplifies when you have to include some third-party code or project. This rules you can find at PHP Fig (PHP Framework Interop Group). I really suggest you to take a look at phptherightway.com. There you’ll find a lot of information to made you a good PHP developer using what we have of better in PHP world.

Let’s take a example. You created a new project using the framework Laravel 4 and you have to use the Amazon API. But Zend Framework has done that for you. They have classes that do the job, and you don’t need to do everything again, just reuse the code that is already developed. You can use its packages to include the Zend Framework’s Amazon API inside your project.

OK! But they are different frameworks! You’re right, but both Laravel and Zend Framework support Composer and you can use it to include sources inside your project. So it’s right! You can add only ZendService\Amazon inside your Laravel project, because Zend Framework was developed as many projects inside one, and all them are developed using Composer too. Autoload will be already configured for you, so, you don’t have to include files, nothing, just call them and PHP take care of the rest.

Comparative

Composer works like Ruby Gems. You have a program that runs in the command line and you can install what you want. You tell Composer to install ZendService\Amazon and it will copy and configure it for you inside your vendor directory. Easy like that.

Installing Composer

Installing Composer is easy. You can get information about Composer installing at getcomposer.org. Go to your new project folder and do the follow:

curl -s https://getcomposer.org/installer | php

This will install Composer for you. Now you have a composer.phar file inside your project main dir. Ok! But this way you’ll can use Composer only for this project. I prefer to move this composer.phar to /usr/bin/ and make Composer globally.

sudo mv composer.phar /usr/bin/composer

Now you have the composer command and you can use it where you want.

Instaling Laravel using Composer

Now you must create our Laravel 4 Beta project. Inside the folder you’ll clone the “develop” branch of Laravel 4 repository:

git clone -b develop git://github.com/laravel/laravel.git
Cloning into 'laravel'...
remote: Counting objects: 19925, done.
remote: Compressing objects: 100% (6296/6296), done.
remote: Total 19925 (delta 14021), reused 19135 (delta 13339)
Receiving objects: 100% (19925/19925), 4.45 MiB | 322 KiB/s, done.
Resolving deltas: 100% (14021/14021), done.
//  listing
ls
laravel
cd laravel
ls
CONTRIBUTING.md  app  artisan  bootstrap  composer.json  phpunit.xml  public  readme.md  server.php  start.php

Well. Now we have Laravel structure copied to our project folder. Now we have to install dependencies for Laravel, using Composer. This will install everything you need to run Laravel.

composer install

You said Composer to install everything this project need. In other words you said it to install Laravel framework, that is a lot of small projects. You can see that exists a composer.json file inside this folder. There you can see everything this project (Laravel) needs to run. Composer will look inside this file and install everything it needs. You have a output like that:

Loading composer repositories with package information
Installing dependencies
  - Installing ircmaxell/password-compat (1.0.x-dev v1.0.0)
    Cloning v1.0.0

  - Installing symfony/translation (dev-master v2.2.0-BETA2)
    Cloning v2.2.0-BETA2

  - Installing psr/log (1.0.0)
    Loading from cache

  - Installing symfony/routing (dev-master de9d647)
    Cloning de9d64780254f45cfa9d9e49602d0a63acf45f75

  - Installing symfony/process (dev-master v2.2.0-BETA2)
    Cloning v2.2.0-BETA2

  - Installing symfony/http-foundation (dev-master v2.2.0-BETA2)
    Cloning v2.2.0-BETA2

  - Installing symfony/event-dispatcher (dev-master v2.2.0-BETA2)
    Cloning v2.2.0-BETA2

  - Installing symfony/http-kernel (dev-master 7d704da)
    Cloning 7d704da42ca56c3f58530a0023a2d532d67005d1

  - Installing symfony/finder (dev-master v2.2.0-BETA2)
    Cloning v2.2.0-BETA2

  - Installing symfony/dom-crawler (dev-master v2.2.0-BETA2)
    Cloning v2.2.0-BETA2

  - Installing symfony/css-selector (dev-master v2.2.0-BETA2)
    Cloning v2.2.0-BETA2

  - Installing symfony/console (dev-master fdb2d93)
    Cloning fdb2d9320106926266a0f7a5a97aab7213e11ad6

  - Installing symfony/browser-kit (dev-master v2.2.0-BETA2)
    Cloning v2.2.0-BETA2

  - Installing swiftmailer/swiftmailer (dev-master 3aec9a3)
    Cloning 3aec9a30346ebf54100a38ba7c71fb8aafae3e9a

  - Installing monolog/monolog (1.3.1)
    Loading from cache

  - Installing laravel/framework (dev-master b3e7df7)
    Cloning b3e7df7894161dcc78d9f2dc11f33f3249c4fdbb

symfony/translation suggests installing symfony/config (2.2.*)
symfony/translation suggests installing symfony/yaml (2.2.*)
symfony/routing suggests installing doctrine/common (>=2.2,<2.4-dev)
symfony/routing suggests installing symfony/config (2.2.*)
symfony/routing suggests installing symfony/yaml (2.2.*)
symfony/event-dispatcher suggests installing symfony/dependency-injection (2.2.*)
symfony/http-kernel suggests installing symfony/class-loader (2.2.*)
symfony/http-kernel suggests installing symfony/config (2.2.*)
symfony/http-kernel suggests installing symfony/dependency-injection (2.2.*)
monolog/monolog suggests installing mlehner/gelf-php (Allow sending log messages to a GrayLog2 server)
monolog/monolog suggests installing raven/raven (Allow sending log messages to a Sentry server)
monolog/monolog suggests installing doctrine/couchdb (Allow sending log messages to a CouchDB server)
monolog/monolog suggests installing ext-amqp (Allow sending log messages to an AMQP server (1.0+ required))
Writing lock file
Generating autoload files

Good. If you take a look in the output, you can see Laravel uses some components of Symfony2, that is another excellent framework that uses Composer too. This show you how Composer is powerful and how you can add inside your project everything using Composer.

If you want to use ZendFramework’s Amazon API (ZendService\Amazon) you can edit the composer.json file and add a new line, saying Composer you’ll need that library too. After that just run composer install again and it will download and install ZendService\Amazon for you. Zend Framework packages can be found in http://packages.zendframework.com.

"require": {
    "zendframework/zendservice-amazon": "2.0.*",
},

To use ZF2 packages you must add a repository too (composer.json):

"repositories": [
    {
        "type": "composer",
        "url": "https://packages.zendframework.com/"
    }
],

Checking everything installed

Now inside your project folder go to vendor directory and there you’ll see a lot of third-party sources, like symfony, swiftmailer and more. You can see too a autoload.php file. That allow you to call these third-party sources using autoload, following the PHP-FIG rules.

Available Packages

As you saw inside composer.json this file contains everything you need to run the project, doing a simple reference like vendor/package. But where are these sources?

Every packages you can use with Composer are stored in Packagist site. There you can browse packages and submit yours too. Simple like that. You have a website where you have a lot of good PHP sources you can use where you want just using Composer for that, and they were developed following the same rules and recommendations.

Conclusion

Composer is the future of PHP. PHP is undergoing good changes to improve the language. Composer is the Dependency Manager was missing, and is here to stay.

I think I could give you a little explanation what you can do with Composer and how it works. If you’ll start a new project, considere using frameworks that use Composer to smooth your third-party sources and make your project organized, like Laravel, Zend Framework 2 or Symfony.

Thanks for reading.

Published by

Junior Grossi

senior software engineer & stutterer conference speaker. happy husband & dad. maintains Corcel PHP, elePHPant.me and PHPMG. Engineering Manager @ Paddle

11 thoughts on “Why you should use Composer and how to start using it”

    1. Hey, thanks for the comment. I understand your point, but using Composer you solve dependencies automatically. Of course you can download packages from PHP Download, if you want to, but IMO Composer makes the requirements process easier. You’re welcome! ?

  1. You all realize that the value of coders and the value of sales will all drop when people can just say “make me program that sell stuff”. Stop trying to make your coding life so easy. It’s just going to put you out on the street. LOL

  2. how to solve this

    composer.phar install

    Loading composer repositories with package information

    [ComposerDownloaderTransportException]

    The “https://packagist.org/packa…” file could not be downloaded: allow_url_fopen must be enabled in php.ini (https:// wrapper is disabled in the server configuration by allow_url_

    fopen=0

    failed to open stream: no suitable wrapper could be found)

    install [–prefer-source] [–prefer-dist] [–dry-run] [–dev] [–no-dev] [–no-plugins] [–no-custom-installers] [–no-autoloader] [–no-scripts] [–no-progress] [-v|vv|vvv|–verbose] [-o|–optimize-autoloader] [–ignore-platform-reqs] [packages1] … [packagesN]

    1. Hi @nameundo!

      Sorry but I think you want to install Composer right? You must have it installed inside your dev machine before start using it.

      Follow this link to install Composer: https://getcomposer.org/dow

      Thanks for the comment 😉

    1. Hi Nikhil!

      I will take a look in this package. Please, send me an email and I’m sure I’ll remember that!

      Thanks for the comment and welcome to the blog.

  3. FYI, for those that have this problem while installing the Symfony2 RAD Edition, the problem was that the version of PHP that I was using (5.3.5) was not high enough for a function in the KnpRadBundle (namely is_a()). I reported this issue to the head programmer of the KnpRadBundle and (amazingly) he fixed the problem in less than an hour (by changing is_a() to is_source_of()).

  4. Hi Junior,

    I am trying to install the RAD Edition of Symfony 2.1 and everything works fine until the end. During the “Generating autoload files” I get the following error messages:

    Generating autoload files

    [ErrorException]
    Warning: is_a() expects exactly 2 parameters, 3 given in /_home/sfprojects/scala/vendor/knplabs/rad-bundle/Knp/RadBundle/DependencyInjection/Compiler/RegisterAppBundlePass.php line 23

    Script SensioBundleDistributionBundleComposerScriptHandler::clearCache handling the post-install-cmd event terminated with an exception

    [RuntimeException]
    An error occurred when executing the “‘cache:clear –no-warmup'” command.

    It appears to be something in Symfony, but I have googled this problem and, although I have found similar issues, cannot find a solution to this problem.

    I am running the following command:

    “composer create-project -s dev –prefer-dist –dev knplabs/rad-edition my-project”

    Also, subsequent attempts to run this command have modules being “Loaded from cache”. Example:

    – Installing symfony/symfony (2.1.x-dev 3e2ff71)
    Loading from cache

    This despite that fact that I am deleting the previous project directory. Do you know where the cached files are stored?

    Thanks in advance.

    Jim

Leave a Reply

Your email address will not be published. Required fields are marked *