Migrating to the Zend Framework
Posted by RichJul 15
I don’t usually blog about work but I think this may interest some people. Late last year we trialled the Zend Framework in a small application. A few months ago we begun the process of converting our main PHP application to it. The migration is still in the early stages so some of this may change rapidly.
We decided on the Zend Framework because it was component based. This was important because we have an existing code base with a regular release schedule. Stopping development while we converted the application wasn’t an option so we needed something that could be integrated slowly.
So how are we doing this?
The first task was to replace the applications module structure with auto loading. While this reduced the number of files that needed to be included to process a request the biggest advantage was making the code more visible to the developers. Prior to this classes were hidden 2, 3 or 4 levels deep in modules. The immediate impact was finding a number of classes that were no longer required.
Next I was expecting to replace some of the more discrete components like logging but it didn’t turn out this way. Because of the applications development schedule work instead focused on migrating from our custom MVC to the Zend Framework MVC.
After writing a bootstrap file we created controllers and actions. The release cycle wasn’t long enough to completely convert the old MVC so the new actions simply call the old code. This approach allowed us convert the front controller (without session management and authentication) in about a day and means that new code can be written using the Zend Framework instead of our old framework. Custom routing was required to emulate some of the old URLs.
The session management and authentication took about to day to sort out on its own. All of the controllers share a common base class. It’s init() function provides some common functionality:
- Initialize the view
- Initialize the session (except the API or AJAX controllers)
- Set all actions as requiring authentication
Starting sessions is done in the controllers init() function instead of the bootstrap file so we can prevent sessions from being started for API calls. This is important because we get a large number of API calls and none of them requires a session. The other important task for init() is to mark some actions as not requiring authentication.
Authentication itself takes place in the controllers preDispatch() function. This provided backwards compatibility with the way the application previously worked.
More recently we’ve begun removing static methods from business logic so that we can use dependency injection to make testing easier. I’ll post about this next time.
This site contains my personal ramblings on Linux, PHP, Java, .NET and anything else that I feel is important.
Leave a Reply