A Zend Framework App in a Single File

Most PHP micro-frameworks I’ve reviewed have some major cons: incomplete namespacing of functions/classes/global vars; doing too much/little; being under-tested; and the worst: forcing a unique (and usually under-documented) code structure that will make it difficult to “graduate” an app into a more full-featured framework. It also seems silly to rely on “micro-optimized” code if performance isn’t your number one goal and you have well-tested libraries sitting around.

So over the weekend, as a proof of concept (and mostly to get better acquainted with ZF’s MVC implementation), I built some minimal classes allowing one to implement a “quick and dirty” Zend Framework MVC app in a single script, with the resulting abomination being relatively easy to convert to a full app. The README shows how such a script might look.

The difference is mainly that views render by passing themselves to functions which output content. In most other ways the controllers/views should behave identically—e.g. you can re-use an action view by calling $this->render('otherAction') in the controller—and Zend’s standard helpers should work (untested!). You can also create “controllers” and “views/scripts” directories structured like a ZF app. The view renderer will try the callback function (e.g. function view_index_index(Zend_View $view) { ... } ) and fall back to default rendering view scripts (e.g. application/views/scripts/index/index.phtml).

Views implemented as functions could be easily be moved to view scripts later (replacing $view with $this).

The idea did cross my mind that using callback rendering for overall layout might have some worthwhile use cases. In an organization with many apps, you could have one layout class for your overall template (with several methods for individual areas), and then extend and modify for each app as necessary. Might be useful…or terrible.

3 thoughts on “A Zend Framework App in a Single File

  1. says:

    I really like the idea of creating small ZF apps in just one file! Good work! I’ll try it this weekend.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.