Zend Framework 1.x and Composer

Composer is a very useful tool for dependency management in PHP. Most of new and cutting edge frameworks like Zend Framework 2, Symfony 2, Laravel 4 etc. are designed to work with it very easily. Zend Framework 1 requires a little modifications.

Install Composer

Easiest way is just:

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

This will download composer.phar to current directory and You’ll be set to go.
If You however want to make composer a system-wide command, it’s pretty easy too:

mv composer.phar /usr/local/bin/composer

Now instead of using php composer.phar [command] , You can do simply: composer [command]

Zend Framework 1.x project

Let’s assume that we already have working ZF1 project and we’d like to switch it to composer.

cd ~/Projects/zf1-project
php composer.phar init

# alternatively if You've made composer system-wide
composer init

Now You’ll have to fill some details about Your current project and then composer will ask You about dependencies. Choose Interactive mode and search for package zendframework1 then enter the number for the line that matches zendframework/zendframework1. Then You’ll have to pass version of the library. If You want to have latest enter 1.*.

Next after another prompt about dependencies just hit return, and for next question answer ‘no’, because we don’t want to define any dev dependencies.

This should create file composer.json which looks similar to:

{
    "name": "genjusz/zf1-project",
    "description": "Zend Framework 1.x integration with Composer",
    "require": {
        "zendframework/zendframework1": "1.*"
    },
    "authors": [
        {
            "name": "Kuba Florczuk"
        }
    ]
}

Install dependencies

Now when we have our composer.json ready it’s time to install our library

php composer.phar install

This will create vendor directory in your project. It’s wise to add this to .gitignore if You maintain Your project with git.

Adjust project files

Last thing to do is modify Your public/index.php file and add this lines just above require_once 'Zend/Application' :

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

/** Composer autoloader */
if (file_exists(realpath(APPLICATION_PATH . '/../vendor/autoload.php'))) {
    require_once realpath(APPLICATION_PATH . '/../vendor/autoload.php');
}

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

This will allow You to use any other libraries that You’ll install via Composer.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

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