« Joel says he does not use exceptions | Main | Poka-yoke and PHP »

Rasmus's no-framework PHP MVC framework

Rasmus Lerdorf, Benevolent Dictator for Life, posted on his blog an entry The no-framework PHP MVC framework:

So you want to build the next fancy Web 2.0 site? You'll need some gear. Most likely in the form of a big complex MVC framework with plenty of layers that abstracts away your database, your HTML, your Javascript and in the end your application itself. If it is a really good framework it will provide a dozen things you'll never need.

I am obviously not a fan of such frameworks. I like stuff I can understand in an instant. Both because it lets me be productive right away and because 6 months from now when I come back to fix something, again I will only need an instant to figure out what is going on. So, here is my current approach to building rich web applications. The main pieces are:

MVC?

I don't have much of a problem with MVC itself. It's the framework baggage that usually comes along with it that I avoid. Even parts of frameworks can be useful as long as you can separate the parts out that you need. As for MVC, if you use it carefully, it can be useful in a web application. Just make sure you avoid the temptation of creating a single monolithic controller. A web application by its very nature is a series of small discrete requests. If you send all of your requests through a single controller on a single machine you have just defeated this very important architecture. Discreteness gives you scalability and modularity. You can break large problems up into a series of very small and modular solutions and you can deploy these across as many servers as you like. You need to tie them together to some extent most likely through some backend datastore, but keep them as separate as possible. This means you want your views and controllers very close to each other and you want to keep your controllers as small as possible.

Goals for this approach

  1. Clean and simple design
    • HTML should look like HTML
    • Keep the PHP code in the views extremely simple: function calls, simple loops and variable substitutions should be all you need
  2. Secure
    • Input validation using pecl/filter as a data firewall
    • When possible, avoid layers and other complexities to make code easier to audit
  3. Fast
    • Avoid include_once and require_once
    • Use APC and apc_store/apc_fetch for caching data that rarely changes
    • Stay with procedural style unless something is truly an object
    • Avoid locks at all costs

Be sure to read it as he explains how to rethink using the "all powerful" frameworks outthere, which makes things much more difficult to quickly get something out the door. I've been thinking about looking at using MVC but it may be best to stick away from trying for the moment and maybe attempt to try MVC with a new language like Ruby on Rails.



TrackBack

TrackBack URL for this entry:
http://www.powertrip.co.za/blog/mt-tb.cgi/505

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)