Composer

A crash course

Christopher Pitt
Laravel 4 Tutorials

--

Composer is a PHP dependency manager. It lets you define the dependencies your project needs. It mainly downloads dependencies from http://packagist.org, but can accept other sources as well.

That guy means business.

Installing Composer

You can download Composer from http://getcomposer.org/
download/
, where you will also find installation instructions for your system. Here’s I’m using the instructions for installing with Curl. I also move the composer.phar file to /usr/local/bin/composer so that I can use it from anywhere in the system. If you you don’t have Curl installed, or would prefer another installation approach, there are instructions for you too!

Some bad-ass instructions there…

Once Composer is installed, you simply need to know the name of the dependency your project requires, and you specify this dependency in a file called composer.json. The composer.json file also allows you to give your project a name, version and a few other details, which can be used to publish your own Composer packages to Packagist.

Once you’re happy with the dependencies you have defined; it’s time to install them. Go to your command line and type composer install. I typically add--no-dev because Composer will install development dependencies by default. If your dependencies will need to build or test themselves, it’s best to leave this out. Installing dependencies may take some time, but any indication of progress is a good sign. Get a cup of coffee and wait it out.

Composer will automatically download nested dependencies, and the more things it needs to download, the longer it will take to download them. When it’s done; it will also generate a decent class autoloader script. You can immediately start to use auto-loaded dependencies by including vendor/autoload.php in your PHP scripts.

Ain’t nobody got time for…not autoloading?!

The libraries you install with Composer are located in the same vendor folder, and Composer will also create a lock file which records the exact versions of your dependencies so that they can be installed in the same way on production servers.

If your favourite PHP framework installs with Composer; it probably includes this file for you.

If you are using a framework that doesn’t include the class autoload script, or no framework at all, you just need to include the vendor/autoloader.php file to be able to autoload your dependencies.

Finding Composer packages

There are many ways to find Composer packages, but the easiest are in the readme.md files of GitHub repositories, in the library or framework documentation and by searching http://packagist.org.

More and more library authors are creating their own composer.json files and submitting their libraries to Packagist. They will usually explain how to use their libraries as a dependency, but it also helps to check any composer.json files you see in GitHub repositories.

Searching Packagist is a little harder if you don’t know what you are looking for, but a search for “http” quickly returned many results (including Guzzle). Give it a shot if you can’t find what you need using GitHub’s search function.

More packages than a FedEx truck.

Submitting your own packages to Packagist

Go to http://packagist.org and sign in. If you haven’t already got an account, you should create it and link to your GitHub account.

Submit. SUBMIT!

There’s a big “Submit Package” button. Click it and paste the URL to your GitHub repository in the text field. Submit the form and you should be good to go.

Conclusion

If you have any questions; be sure to check out the forums, FAQ and help documents for the thing you’re struggling with.

https://packagist.org/

http://getcomposer.org/

If you found this tutorial helpful, please tell me about it @followchrisp and be sure to recommend it to PHP developers. All PHP developers.

--

--