Developing software in the Real World

Zend Framework Front Controller

Zend have finally released a preview of their new framework at

I’ve started having a play and their implementation of a Front Controller is quite similar to what I’ve been playing with. This isn’t a total suprise as there aren’t that many ways to do a Front Controller :)

The documentation on the Zend/Controller directory is completely non existant, so I’ve worked out the basics by reading the source. Fortunately, the source isn’t too hard to read!

To get something going, I set up my directories like this:

.htaccess turns on mod_rewrite to redirect every url to index.php (if the url doesn’t point to an existing file or directory):

wwwroot/index.php sets up the Front Controller…

Note that Zend::loadClass() and Zend::loadInterface() assume that the Zend/ directory is on the path…

You also need a controller, so I created wwwroot/controllers/IndexController.php:

Not exactly complicated, but it tells me what’s going on.

Very quickly, I discovered that the Zend_Controller_Router assumes that my index.php lives in my webserver’s document root. That isn’t the case on my test machine, so I decided to write my own router. I chose to put this in lib/rka/Router.php and called the class RKA_Router so that I could take advantage of one of the Zend::loadClass();

Being lazy, I just cloned Zend_Controller_Router and altered it a little. This is my change:

The only bit I changed are commented with “//RKA”. Essentially, I work out the path to the index.php and then remove it from $_SERVER[‘REQUEST_URI’] so that I can have urls of the form http://localhost/test/wwwroot/{controller}/{action} rather than http://localhost/{controller}/{action}

I had to modify index.php by changing:


and obviously:


And that’s about it. The Zend Framework’s front controller works fine. Any public non-static function within a controller class is considered an action. So http://localhost/index/test will call the IndexController::test();

Next up is to work out how to tie this in with the Zend_View class!

(Update: WordPress is doing something funky with the double quotation marks in the PHP code, they aren’t really escaped!)

16 thoughts on “Zend Framework Front Controller

  1. I guess Zend::load_class() is meant to be used in an __autoload function. This would it also make easier to change the controlling classes like your router.

  2. @Gom:
    Ooh! I'll have a look. Thanks!

    __autoload()… that's a good idea. I'll try that as it'll save having to keep track of stuff.

    Another thing I've noticed is that that Zend::loadFile() calls Zend::isReadable() which does an fopen() followed by an fclose(). I wonder if there's any performance issues with doing that for every file you want to include() ?

  3. Nice notes, thx. The controller looks very interresting.

    Btw: have a look at css overflow directive :)

  4. Thanks soenke! Can you tell that I'm a programmer and not a designer! It does show up that this template doesn't have enough "width" to it though…

  5. Autoload doesn't work with Zend::loadClass() because it calls class_exists() to check that the class isn't already loaded:

    if (class_exists($class)) {

    This results in the __autoload() being called which calls Zend::loadFile() which then gets confused :)

    Workaround is to modify to:
    if(class_exists($class, false)) {

  6. Am i looking at this correctly? Do i need a contoller function for every static link i place in a page. For example: several non-dynamic informational pages would each need it's own controller entry for a link to work. Seems like a lot of code to replace a href but i don't see a way around it with the mod_rewrite.

  7. You are looking at it wrong. The rewrite rule on this page will serve a file if it exists, but if it doesn't, then it will serve index.php.

    Incidentally, the rule in the ZF manual is different…

  8. Ok, i added an RewriteCond to exclude a directory and added all static pages in that directory which worked fine. Thanks.

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *