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

    • Automatically upload screenshots in XFCE4
    • Zend Framework full page cache tips
    • No more Wordpress
    • Xdebug is full of awesome
    • Creating a chat bot with PHP and Dbus
    • A year in review: 2011
    • Notes on shell scripting
    • Listening to Dbus signals with PHP
    • Configuring 2 monitors with xrandr
    • A quick note on Dojo's data grids and dojox.data.HtmlStore
  • Recent Comments

    • Robert on Zend Framework full page cache tips
    • Stephen S. Musoke on Zend Framework full page cache tips
    • David on Zend Framework full page cache tips
    • Anon on A quick note on Dojo's data grids and dojox.data.HtmlStore
    • James on Communicating with Pidgin from PHP via D-Bus
    • Robert on A Zend Framework 2 EventManager use case
    • Jowee on A Zend Framework 2 EventManager use case
    • Jurian Sluiman on A Zend Framework 2 EventManager use case
    • Jurian Sluiman on A Zend Framework 2 EventManager use case
    • djozsef on Webkonf 2011 recap
  • Tags

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

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

    • 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.
Comments: 6 comments.

Comments: 6

  • Hari K T

  • April 16th, 2011
ie g8 :) , I too was not aware of this way. Thanks ;)
  • Grayson Koonce

  • April 16th, 2011
Had no idea. Another thing, I think little known, is if you have a controller named FooBarController - it can be accessed via the URI /foo-bar. When I combine this with your post - you can have something like so: /sub_foo-bar to get at a controller called FooBarController in the Sub directory. Neat stuff! I wish there were ways to configure the separator for both scenarios without needing a route...
  • Scott Lively

  • April 18th, 2011
You can also use modules. However, there is a step of config that needs to be done which is to specify to the front controller that there's an additional controllers directory. For example, $frontController->setControllerDirectory(array( 'default' => APP_DIR.'/controllers', 'admin' => APP_DIR.'/modules/admin/controllers', )); Uri would then be module/controller/action
  • HernanF

  • April 19th, 2011
Module Rulz. Remember always extends class Bootstrap extends Zend_Application_Module_Bootstrap { #default module class IndexController extends Zend_Controller_Action{ #xxx module calss xxx_IndexController extends Zend_Controller_Action{ and folders like application/default/controllers application/xxx/controllers and the others folders too. ;) good luck.
  • Robert

  • April 19th, 2011
@Scott, @HernanF yes, I'm quite aware of the modules, thanks ;)
  • Adam Lundrigan

  • May 30th, 2011
Robert, Nice post! Explains the concept of sub-directory controllers very well. I wasn't a fan of having underscores in my URIs, so I created an application resource plugin which automates the setup of the necessary custom routes to turn the URI for accessing Sub_FooController from /sub_foo into /sub/foo. It's still a work in progress, but the code is available on my GitHub if anyone would like to try it out: https://github.com/adamlundrigan/zf1-nestedcontrollers

Leave a Reply

Robert Basic © 2008 — 2012
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