Using Symfony Dependency Injection with Zend_Application
As a follow-up to my recent post on Dependency Injection containers and Zend_Application I was eager to find out if its possible to integrate the new Symfony Dependency Injection Container into the Zend Framework. To my suprise its possible without having to make any changes to one of the two components. An example use-case would look like:
$container = new sfServiceContainerBuilder();
$loader = new sfServiceContainerLoaderFileXml($container);
$loader->load(APPLICATION_PATH.'/config/objects.xml');
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/config/application.xml'
);
$application->getBootstrap()->setContainer($container);
$application->bootstrap()
->run();
Resources instantiated by Zend_Application are then injected into the container by the name of the resource and are given the resource instance. Any object from the Symfony container can then use these dependencies in their object setup. The only drawback of the integration is the fact that the Symfony Container is case-sensitive in regards to the service names but Zend_Application lower-cases all service before injecting them into the container. The following code is a restatement of my previous example of a MyModel class which requires a Zend_Db and Zend_Cache constructor argument.
$container->register('myModel', 'MyModel')
->addArgument(new sfServiceReference('db'))
->addArgument(new sfServiceReference('cache'));
Access to a MyModel instance with its dependencies is then granted through the call $container->myModel throughout the application. Make sure to call this after running Zend_Application::bootstrap, so that the Resource dependencies are injected first.
Category: PHP,
Comments (4)
Tags for this Article
Comments (4)
I'm still a newbie with Dependency Injection and have this question I can't find an answer for: how do I get the main container in a class which has no reference to Application instance?
Eg: I have a data mapper which creates an entity. The configuration of the entity with according dependencies is defined in the man config. My intention would be to call the container and get a new class from it. The container is stored in the Bootstrap but I have no reference for it in my mapper class.
Thank you in advance. User: sas171 - Date: 2009/07/08 03:08
I have just been using the Yadif container with ZF, would you use the Symfony instead?
Thx
Keith User: mute - Date: 2009/07/16 11:00
http://blog.starreveld.com/2009/11/using-symfony-di-container-with.html
It includes flexibility for hooking in other containers (Yadif, etc.), fallback on the classic Zend DI container, and code to dump/load from the generated PHP code. User: dolf - Date: 2009/11/15 09:45
Post A Comment
Back To The Top


One way you could simplify this somewhat for re-use is to extend the common bootstrap class and override the getContainer() method to lazy load your DI container instead (including configuration, potentially). User: mweierophinney - Date: 2009/07/04 15:32