Using the Zend Framework with Symfony 1.1

Tuesday, 21 October 2008

I've written about using the Zend Framework within Symfony before but things have changed a bit in Symfony 1.1 so I wanted to post an update with a new Symfony 1.1 compatible approach to loading and using the library from within Symfony. The biggest change that has happened since my previous article is that the sfZendFrameworkBridge has been removed from Symfony. This is actually a Good Thing since, as you'll see in a moment, there is now a more elegant approach to loading external libraries.

Most of the steps are the same as those outlined in the afore-linked article so you may want to read it before going further. The main difference will be how we get the Zend Framework files to autoload.

Getting the Library

This article assumes that you've placed the Zend Framework files in <sf_project_root>/lib/vendor/Zend just as we did the last time around. Note though, that the Zend Framework subversion repository went through a bit of a restructure, so if you're linking to lib/vender/Zend via svn:externals you'll want to use this updated format on lib/vendor:


Linking the Library to Symfony

Now, to link the Zend Framework and allow auto-loading of class files, all you need to do is add a few lines to your application's configuration class file. If your app is named frontend then the file you'll need to edit is located at apps/frontend/config/frontendConfiguration.class.php.

Modify your initialize() method to include the following:

public function initialize() 
  $sf_zend_lib_dir = sfConfig::get('sf_lib_dir').DIRECTORY_SEPARATOR.'vendor';
  spl_autoload_register(array('Zend_Loader', 'loadClass'));

… and that's it. Clear your cache (./symfony cc from your project root) and you should be good to go. Creating an instance of a Zend Framework class is as easy as something like this:

$pdf = new Zend_Pdf();

Zend_Pdf will be auto-loaded for you.

The information in this article was gleaned from a post by Dustin Whittle and also one from Kris Wallsmith.