• Subscribe to the RSS feed! RSS icon
  • Subscribe by Email
  • home
  • blog
  • dev
  • Recent Posts

    • Eight years of PHP
    • Learning English
    • Saturday night hack - coords
    • When a package update goes wrong
    • Frontend testing with phantomjs and casperjs
    • Gnucash 4.2 with SQLite3 on GNU/Linux
    • A monkey with a banana
    • 2012 - 'twas a fine year!
    • Let's learn Astronomy!
    • Unit testing Zend Framework 2 modules
  • Tags

    php, about, random, framework, zend, example, ubuntu, zend framework, blog, site, python, book, conference, linux, me, wordpress, apache, hack, hex, introduction, lamp, longboard, open source, review, script, setup, signals, zend framework 2, ape, community, contributing, dbus, dojo, events, life, mysql, netbeans, pidgin, plugin, pyqt
  • Categories

    • Blablabla
    • Development
    • Free time
    • Places on the web
    • Programming
    • Software
    • Uncategorized
  • Archives

    • April, 2013
    • March, 2013
    • February, 2013
    • January, 2013
    • September, 2012
    • August, 2012
    • July, 2012
    • June, 2012
    • February, 2012
    • January, 2012
    • December, 2011
    • November, 2011
    • October, 2011
    • September, 2011
    • August, 2011
    • July, 2011
    • May, 2011
    • April, 2011
    • March, 2011
    • January, 2011
    • December, 2010
    • November, 2010
    • October, 2010
    • July, 2010
    • June, 2010
    • April, 2010
    • February, 2010
    • January, 2010
    • December, 2009
    • November, 2009
    • October, 2009
    • August, 2009
    • May, 2009
    • March, 2009
    • February, 2009
    • January, 2009
    • December, 2008
    • November, 2008
    • October, 2008
    • September, 2008

Grouping Zend Framework controllers in subdirectories

by Robert Basic on April 16th, 2011

Thanks to a discussion on the Zend Framework mailing list I learned about a new feature, a feature that allows for grouping action controllers in subdirectories! Well, this is more of an unknown and undocumented feature than new, as it is the part of the framework for at least 3 years.

Subdirectories example

Subdirectories example

Why am I so hyped about this? Because it allows for better code organisation on larger projects. Heck, it might be useful on smaller ones too. For example, if a module Foo has both a backend and a frontend, what I was doing so far was to have the file and class names prefixed with an Admin prefix for the backend files and no prefix for the frontend files, so I can actually see what file belongs where. This can go out of control quite easily.

On the other hand, with this grouping of controllers I can make an Admin directory and just place all the backend related controllers there. Easy, clean and much more easier to see what's where. In my opinion at least :)

Example

Best part is that this feature requires no additional configuration. Create a subdirectory under the controllers directory and place the controller file under that subdirectory. In that pretty screenshot image you can see a FooController.php in the directory called Sub; the class name in that example is Sub_FooController and is accessible via the sub_foo/controller URI. The corresponding view files should be placed in views/scripts/sub/foo/ directory.

A few notes on this:

  • the subdirectory separator in the URI is the underscore and not the slash.
  • The subdirectory name is uppercase: Subdirectory, not subdirectory. In the URI it's lowercase.
  • The view directories for these subdirectories are lowercased, and not uppercase.

The uppercase subdirectory sounds weird, but meh. If needed, the underscore can be "changed" to the slash with a route.

I think the level of possible subdirectories is not limited, but I really can't see an use-case for more than one subdirectory.

Happy hackin'!

Tags: controllers, zend framework.
Categories: Development, Programming.
Robert Basic © 2008 — 2013
Design & graphics by: Livia Radvanski
Coded by: Robert Basic
Home page last updated on November 30th, 2009.
Frameworks used: Zend Framework, Dojo, 960 Grid System