Pragmatism in the real world

Zend Framework Controller Docs!

In case you missed it, 0.1.2 is out with documentation for the controller!

As I’m at work, I don’t have a have a lot of time, but here’s the highlights:

Directory Structure

/application
  /models
  /views
  /controllers
/document_root
  /images
  /styles
  .htaccess
  index.php
/library
  /Zend

This isn’t far off what I invented. The only significant difference is that I placed “document_root” under application and called is “wwwroot”. The main reason is that I imagine that I’m going to need the files in /application and /document_root in the same SVN tree, with the stuff in /library in my path. It all depends really; maybe I should keep the entire Zend Framework in each SVN tree, but that strikes me as a pain when you have 100 ZF sites on your books.

Server Configuration

Note that the suggested .htaccess file prevents you from having another php program running in a subdirectory of your document_root folder. So.. if you want /blog to be a wordpress installation, then you’ll probably want a more advanced .htaccess rule. Also note that you can’t store files for direct download like PDFs with the default rule.

Subclassing

Seems like the way I have subclassed Zend_Controlelr_Router and Zend_Controlelr_Action is the “correct” way. I’ve also nearly followed the correct naming convention :)

I note with satisfaction that my changes agree with the bit that says:

Please be careful when overriding significant parts of the system, particularly the dispatcher. One of the advantages of Zend_Controller is that it establishes common conventions for building applications.

All I do it automagically tie up the view to the controller and route to a directory of the virtual host. In passing, I see that a better router is on the way. With any luck, that’ll remove the need for Akrabat_Route.

Integrating the View

Unfortunately, there is no documentation on integrating the view with the controller. Chris Shiflett ‘s PHP|Architect tutorial suggests creation and rendering the view directly in the controller:

< ?php
class IndexController extends Zend_Controller_Action
{
    function index()
    {
        $view = new Zend_View();
        $view->setScriptPath('/path/to/views');
        echo $view->render('example.php');
    }
}
?>

As I commented in my thoughts, the Zend_Controller_Action class allows for forwarding from one action to another. If both actions want to supply data in the final output to the browser, then we need to do something more clever.

There is also the problem what we are repeating the code to setup the View and render in every single action function. This is clearly not optimal, especially as 99% of the time we can use convention to work out the name of the template that a given controller action will use. In my code, I’m using views/{controller_name}/{action_name}.tpl.php, but any convention will work.

To my mind this bit is one of the more important bits as the convention here is as important as the directory structure advice; everyone knowing where to find the correct view file for a given acton in any Zend Framework application will be very powerful.

I suspect that some more work will be done :) probably after the new router is released.

Conclusion

So, to sum up, the stuff that I’ve done so far is still relevant given the released docs. I’ll update my code shortly to match the suggested directory structure. I think that a system for integrating the view and the controller is still needed and until an “official” one is worked out, I’ll stick with my one as it’s a pretty flexible solution that hides it all away nicely.

4 thoughts on “Zend Framework Controller Docs!

  1. *grin*

    I noticed! I've started looking through the diffs from 0.1.1. I'll post some highlights at some point.

  2. Just a note on some .htaccess hackery. If you use the following rules, Mod_Rewrite will only call the RewriteRule when the requested file or directory does not exist.

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

    MUCH MUCH easier! I had known about this before, but I personally pulled this little snipped from the Drupal .htaccess file.

  3. Yes, that's nearly exactly what I use:

    RewriteEngine on
    RewriteOptions Inherit
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule (.*) index.php?apache_path=$1 [L,QSA]

Comments are closed.