Setting up ZF2's Zend\Tool side by side with ZF1

If you want to play with the development versions of Zend Framework 2.0, then it's handy to be able to create ZF2 projects using the Zend\Tool command line tool.

Rather unhelpfully, ZF2's Zend\Tool uses the same ini file (~/.zf.ini) as ZF1's Zend_Tool and the same zf.sh script filename, so you can't just put zf2 on to your path and it'll all just work.

I am assuming that you're like me and have production sites using ZF1, so you probably don't want to mess up your current zf.sh usage. This is how I implemented side-by-side ZF cli scripts.

1. Install ZF2 somewhere

I like to install in /usr/local/include. From the command line type:

cd /usr/local/include
git clone git://git.zendframework.com/zf.git zf2

Don't forget to periodically update it with:

cd /usr/local/include
git pull origin master

(And don't be surprised when it breaks whatever you've already coded in ZF2.dev!)

2. Create .zf2.ini

You need an ini file for ZF2, so call it .zf2.ini and store it in your home directory next to .zf.ini. You need to set the correct include path so that Zend\Tool's zf.php can find your ZF2 installation. From the command line type:

echo 'php.include_path = "/usr/local/include/zf2/library:/usr/local/include/Akrabat/zf2/"' >> ~/.zf2.ini

This creates the .zf2.ini file with the correct include_path set up.

3. Create a zf2 alias

Update your ~/.bash_profile to set up an alias to the ZF2 zf.sh script.

Using a text edit, add this line to the end of ~/.bash_profile:

alias zf2='export ZF_CONFIG_FILE=~/.zf2.ini; /usr/local/include/zf2/bin/zf.sh'

Restart your terminal or type source ~/.bash_profile

Now you can type zf2 to run ZF2's zf.sh and Zend\Tool will run and not be affected by any ZF1 configurations you may have!

ZF1 and ZF2's cli scripts both work!

If you would like to comment on this article, please ping me on twitter.
If your response won't fit into 140 characters, write a blog post and then ping me!