After a little more than a week since I started the upgrade to ZF, I have to say I am enjoying working with it, although it certainly gave me headaches. Admittedly, most of those headaches were a result of my own questionable coding decisions from the existing ZF1 application. Still, it was a bit frustrating at times.
A big issue I ran into is my dependency on Zend_Registry. I was using this not only to inject the Doctrine entity manager wherever I needed it, but also to inject some other objects that was pretty much needed everywhere I was working. I realized using it in that manner wasn’t the best idea, but it kept my object instantiation and method calls from become multiple lines long. As this class was removed in Zend Framework 2, I had to scramble to find another solution, which meant having to alter my current code.
For the Entity Manager part, I was able to use ZF2′s Service Manager functionality. By creating a service factory for each of my services, I was able to inject the Entity Manager into it upon its set up. So by using Zend’s Service Locator, I’m able to create an instance of my needed service, with the Entity Manager already available.
$customer = $this->getServiceLocator('myproject-customer-service');
Unfortunately, I was never quite able to get another feature of ZF2, the Dependency Injector (Zend\Di) to work. It just isn’t set up to do what I needed it to do, and from documentation it sounds like something that may not be the best for use in production code anyway. For now I’m injecting most of my dependencies the old fashioned way: through either the class constructor or setter methods. If anyone has any suggestions about how to inject a couple of objects that are instantiated upon successful authentication into my code without doing it in every single action in every single controller, I’d much appreciate them.
Beyond that, I’ve also encountered the changes to Zend\Validator and Zend\Config. Neither of which are changed greatly (although Zend\Config requires an external reader for YAML now), and were pretty quick to switch over in my existing code. Beyond a single onDispatch call I’ve yet to really dig into the eventManager, something I’m very interested in learning. As for the other components of ZF2, I’ve yet to deal with them, although I expect to get into Zend\Permissions soon.
Although the switchover hasn’t been as painless as I’d hoped, I’m still glad to be doing it. The things that have caused the most problems have been things that require me to think about the design of my code, and the end result is a stronger application that will be easier to maintain. Some of the code I’m transferring over needs a good refactoring, and I’m glad to have the chance to do that.