by Adam Brett

Zend Framework 1.x and Composer

This article was published on Friday, May 10, 2013 which was more than 18 months ago , this means the content may be out of date or no longer relevant. You should verify that the technical information in this article is still current before relying upon it for your own purposes.

Using Zend Framework 1.x with composer is fairly straight forward, even if not immediately obvious. First, let's create a new Zend Framework 1 project in the standard way, using the zf tool:

zf create project zend-composer
cd zend-composer/public
php -S 127.0.0.1:8000

Now visit http://127.0.0.1:8000 in your browser and you should see the familiar "Welcome to the Zend Framework!" page.

Welcome to Zend Framework

The default project is loading Zend Framework from your include path, so first off, let's break that. Edit ./public/index.php and comment out the get_include_path() line, like so:

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    // get_include_path(),
)));

Refresh your browser and you should now be getting an error similar to this:

Zend missing include path

That's good, now we're going to install Zend Framework via composer and add that to our include path. Close the PHP server using Ctrl+C and run the following1:

cd ..
composer init

Fill in the details for your project until you get to:

Would you like to define your dependencies (require) interactively [yes]?

Press return for yes, then search for zendframework1:

Search for dependencies

Enter the number for the line that matches zendframework/zendframework1 which for me is 0, and probably will be for you too.

Next, enter the version you require, use 1.* for the latest 1.x version2.

At the next Search for a package []: prompt, press return, we don't want to define any dev dependencies yet, so type no at the next prompt then return to confirm generation.

You should end up with a file in your project root called composer.json that looks similar to this:

{
    "name": "adam/zend-composer",
    "require": {
        "zendframework/zendframework1": "1.*"
    },
    "authors": [
        {
            "name": "Adam Brett",
            "email": ""
        }
    ]
}

Now type composer install to get things started. Composer will go away and download a fresh copy of the latest version of Zend Framework and place it in your projects ./vendor directory.

We can check like so:

$ tree vendor -L 1
vendor
├── autoload.php
├── composer
└── zendframework

Great. We're almost done. Now remember that line we commented out? We need to update that to point at our new Zend Framework download. The actual library lives in ./vendor/zendframework/zendframework1/library so open up ./public/index.php and change the commented line to match:

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    realpath(APPLICATION_PATH . '/../vendor/zendframework/zendframework1/library')
)));

Now if we restart the PHP server using:

cd public
php -S 127.0.0.1:8000

And refresh the site in our browser, we should see the "Welcome to Zend Framework" page again. Success!

There's one last thing to do if you want to consume other composer packages in Zend Framework, and that's to add composer's autoloader. Edit ./public/index.php again and add the autoloader around line 17, just above the require_once for Zend_Application:

require_once realpath(APPLICATION_PATH . '/../vendor/autoload.php');

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

Update:

If you've followed this guide along step-by-step, the placement of your require_once for the composer autoloader is really important, because we totally overwrite the include_path in a previous step.

If you include the composer autoloader before that step you will overwrite any include_path that composer sets, and will run into a similar issue to the one found here.

To avoid that problem simply make sure you include the composer autoloader after (un)setting the include path!

Now repeat these steps in ./tests/bootstrap.php and we're all done!

Further Reading


  1. I'm assuming you have composer installed system wide 

  2. Zend Framework 2 is a separate composer package, rather than a version 2.x, so it's impossible to install ZF2 via the zendframework1 package. 

For exclusive content, including screen-casts, videos, and early beta access to my projects, subscribe to my email list below.


I love discussion, but not blog comments. If you want to comment on what's written above, head over to twitter.