Zend Framework 1 Extension Library and Composer

Maybe I am a bit late to get on the Composer bandwagon, but a colleague was adamant about it and convinced me to use Composer in our projects. The benefit of using Composer with your PHP applications is that it will download any library dependencies for you, and take care of the autoloading of classes. So I enabled Composer in my Zend Framework 1 Extension library, with a Zend Framework 1.* dependency. Read on how to get the libraries using Composer!

Get Composer

If you don’t have Composer yet, then get it with the following command:

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

This tells the installer to put it in /usr/local/bin/composer so that it is directly available in your shell.

Installation

So let’s get started and create a new ZF1E project using Composer! Because I don’t have a stable version of ZF1E out yet, you’ll need to have a composer.json file that has the following content:

{
    "minimum-stability": "dev",
    "prefer-stable": true
}

With this composer.json file in place, all you need to do is run the following:

composer require wthielen/zf1e

This will fetch the ZF1E library and its dependency – Zend Framework 1 of course – from packagist.org. You’ll see a new vendor directory, which is where Composer puts the libraries in.

Preparation

Now all we need is a default Zend project directory structure, which we can get by creating a project:

./vendor/zendframework/zendframework1/bin/zf.sh create project .

It will complain about unknown PHPUnit classes, but that is okay. At least you’ll have an application directory and a public directory as your document root.

Since we are using Composer, we want to use its autoloader instead of Zend’s built-in autoloader. To change this, tell the application to include vendor/autoload.php in the public/index.php file, by changing the following lines:

/** Zend_Application */
require_once 'Zend/Application.php';

into:

/* The Composer autoloader */
require_once '../vendor/autoload.php';

This is all to get your Composer-based Zend Framework application running!

What are your thoughts?