Wednesday, June 10, 2009

Integrating CodeIgniter with Legacy PHP applications (Part I)

One of the products that I do maintenance on, and occasionally do new development work on, at work, is a monster PHP thing.  It's web based, its procedural, if you're lucky, and its a bit of a mess.  It uses register_globals=on, and quite alot of the business logic, view and presentation is present all in the one program for any given particular screen.  Someone else wrote it originally, and we've never really recovered enough to turn it around and make it better.

We've not done any active development work on it for about 9 months now, but there are some changes coming.  My past few projects have been done using CakePHP.  I really like MVC, and I'd like to drag this monolithic monster kicking and screaming into the realm of modern programming practices.  At the very least, it will keep the project work fresh, and make it something interesting to work on, as well as being maintainable.

As I said, it's a monster, and a full conversion to something easier to maintain is not something that the client is going to dish out for.  So converting to CakePHP is off the table.  What I need is a light weight MVC framework for PHP that is flexible and be worked into the legacy application one bit at a time.  The framework also needs to be in active development, and supported.

So I went for a wander on Google and found CodeIgniter (CI).  It would appear to be a light weight MVC framework for PHP, where you can choose to drink as much or as little of the kool aid as you want.  It seems to have an active development team, and an active community of folks using it.

I didnt go for CakePHP in selection because its just too heavy.  Its an all or nothing framework, and would take too much hacking around to make it work in the environment.

So I took a cut of the project in SVN so I could integrate CI.

The next thing I did was have a look around to see what I could do to integrate legacy PHP applications with CI.  By legacy, I mean old-ish code, nasty stuff thats all procedural, uses register_globals and is a general mess.  I eventually came across this article called Calling CI models from outside script.  It's pretty good article with some good pointers to other articles discussing how to make use of CI models without making your whole program CI.  However, wasnt quite what I wanted.

What I wanted was an introduction on how to use CI libraries in regular code.  My specific goal, and test case, was to replace the session control in the legacy application with the session control available in CI.  I figured, once I have that sorted, the rest should fall into place.

Part II will cover a little about the layout of the legacy application, and how I integrated CI session control into it.


No comments: