March 20, 2012

Secret weapons for maintaining old PHP sites: Mustache.php + PHPActiverecord

It's 2012, so there's getting to be no excuse for it, but sometimes, we find ourselves having to use PHP for tasks beyond editing Wordpress themes. Thankfully, there are options when it comes to maintaining legacy PHP sites.

The list of ORMs and of templating lanuages for PHP is quite long, but here are the two I've settled on as the best choice for a drop-in addition: PHPActiveRecord and Mustache.php

ORM in a hurry

PHPActiveRecord could scarcely be easier. You will be required to perform some initialization before you use it, but as demonstrated in the quick start guide, this initialization is a little bit of boilerplate surrounding an SQL database connection string. It seems to support MySQL, PostgreSQL, and SQLite at least.

After you connect to the database, you'll want to create a model. This is as easy as extending the ActiveRecords\Model class. You may have to do some extra configuration; for example, if your ID field isn't called "id," you'll need to specify it as a static class variable. Most importantly, you can do this on an existing database table; for our purposes -- adding much-needed structure to php sites that formerly relied on ad-hoc SQL querys cobbled together within the template -- this is ideal.

PHPActiveRecord is adequately documented, although it feels a bit sparse at times. Happily, the patterns for using it are consistent enough that you should get the hang of it quickly.

Mustache.php

The Mustache family of templating languages prides itself on separating logic from display. Of course this is impossible; Mustache instead sports a spartan set of logic features, in the form of a dual-purpose if/loop blocking mechanism. You are expected, or at least well-advised, to drop the template itself over a class providing the template variables; in this way, you can really put all substantial logic in one place and display in the other.

It couldn't be easier to use Mustache, although it might be a hard sell if you're used to PHP just inserting itself into HTML. Instead of using PHP itself as a templating language -- it outgrew that long ago -- you create a template file, read it in, instantiate a view class (extending Mustache), and call $view->render($template), where $view is the instance and $template is the template as a string.

Even if the only advantage using a templating language offered was to keep PHP out of HTML, it would be worthwhile; it is much easier to maintain templates created in this way.

By your powers combined

Using these two tools has allowed us to streamline development on a recent project. Both of these libraries are self-contained, so dropping them in was easy. Once you have the Models and the Views all assembled, you can focus your attention on the glue code of the Controller. Do this how you like; this was implemented in an app that had an "action" parameter, which made the controller a case of handling routing in the constructor and implementing the resources as methods, like some sort of ghetto Rails.

Adding the Create view took about the same time as without, but subsequently adding Read, Update and Delete was enormously faster than it would have been attempting to spaghetti things in place from the start.