ZF1.11 + Doctrine 2 + Symfony DI Integration

Last week, I blogged about looking for a new application framework to use and more importantly the key things that I would want in one. After reading about the Zend framework, Symfony and Laravel and finding myself hesitant to use any one, I thought to myself why not get the best of each framework? So that’s what I did.

I retained Zend for its MVC. I like Symfony for its DependencyInjection component and integration with Doctrine and thought to myself why not just integrate them in? Finally, although I loved Laravel for its File API and Rest Controller, I settled to look for existing solutions within the Zend Framework. This is especially true with the Rest Controller. Turns out with a little tweaking you could actually have a Rest Controller in Zend version < 2. Also to my surprise, Zend comes with a File Api as well that doesn’t use static methods and is fully Object Oriented.

Loading them all together: Zend_Loader_Autoloader

Autoloading was the foremost issue on my mind when I thought about doing this especially because Symfony and Doctrine use PHP namespaces already and ZF 1.11 doesn’t as of yet. Turns out, this is easily handled already by Zend_Loader_Autoloader:

The key is to just register the namespace with the Autoloader and just as important: to make sure the libraries are in the include path and the directory structure follows PSR-0.

Getting hold of the D.I container
With that issue out of the way, the next thing concern that came to my mind was grabbing hold of the D.I container in my controllers so I can get my services. This can be done through Zend_Application’s Bootstrap like so:

Then, in the action controllers, getting the container and a service was as easy as:

Future work
I know. The current method of retrieving the container leaves much to be desired. Hopefully when the stable release of the Zend Framework 2 comes out, this can be easily resolved by creating the D.I container as a Zend framework a plugin.

Having said that, I will also work on porting this setup to ZF2 very soon.

Okay enough talk now, head on on over to Github to check it out.

Leave a comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>