ZF Multi-Level Controller Directory - Action controller name formats; keyname, url segment, folder, file, class name

ok..here are the results of some more experimentation that i have been doing with Zend Framework’s Front Controller.

I was wondering if it was possible to be able to have  sub directories within a controller directory.

As far as i can tell…yes its possible.

I set up a route in my bootstrap as follows  :
{{{

//capitalize the controller directory to keep it in line
//with one of the zend naming conventions
$front->setControllerDirectory(’Controllers’);

$router = $front->getRouter();

$router->addRoute(
‘trial’,
new Zend_Controller_Router_Route(’trial/:username/:width’,
array(’controller’ => ‘backend_trial’,
‘action’ => ‘index’)));

}}}

then I created a controller script within a subfolder of my controller directory

{{{
/Controllers/Backend/TrialController.php
}}}

{{{

<?php

class Backend_TrialController extends Zend_Controller_Action {
public function indexAction() {

$this->_helper->viewRenderer->setNoRender(TRUE);

echo(’i am Backend_TrialController::indexAction’);
}
}

}}}

I Visited the URL : example.com/trial/jack/thirty

Prints:
{{{
i am Backend_TrialController::indexAction
}}}

If I comnment out the line that prevents view rendering, then i get the exception:

{{{
Fatal error: Uncaught exception ‘Zend_View_Exception’ with message ’script ‘backend/trial/index.phtml’ not found in path (./views/scripts/)’
}}}

Which gives us the path to the expected view script:
/views/scripts/backend/trial/index.phtml
—–

I  reset it to prevent view script rendering for the rest of my tests.

———–

Testing Subfolder Naming Convention

Then I was trying to see what happens if i made the subfolder  name lowercase:
{{{

Changed:
/Controllers/Backend/TrialController.php
To:
/Controllers/backend/TrialController.php
}}}

Which gave me an Exception:
{{{
Uncaught exception ‘Zend_Controller_Dispatcher_Exception’ with message ‘Invalid controller specified (backend_trial)’
}}}

Which implies that subfolders have to be capitalized.

——

Next experiment : Testing URL to Subfolder Mapping

I changed the sub folder back to capitalized :
{{{

Changed:
/Controllers/backend/TrialController.php
Back To:
/Controllers/Backend/TrialController.php
}}}

Then visited:

example.com/Backend_Trial
example.com/backend_trial

both of which worked - outputting:
{{{
i am Backend_TrialController::indexAction
}}}

———-

So, to summarize:

Controller Class Name Format

(case sensitive - according to another one of the zend naming conventions)
Format: class Backend_TrialController {}

Folder structure / Names -case sensitive

Format:  controllers/Backend/TrialController.php, or

if  $front->setControllerDirectory(’Controllers’);

Format:  Controllers/Backend/TrialController.php

URL (case insensitive):

Format: example.com/backend_trial, or
Format: example.com/Backend_Trial

Controller Name

Appears to be case insensitive when setting in router_route::_construct() $defaults array.

Format: ‘controller’ => ‘Backend_Trial’, or
Format: ‘controller’ => ‘backend_trial’

View Script File Name /Path

/views/scripts/backend/trial/index.phtml

———

All this might be documented somewhere in the framework docs.

Please add a comment if you know where it is.

Note : i have not tested two levels deep with modules.

TODO:
One of these days, if I have the time, I’ll write up a summary of Zend naming conventions - as they don’t appear to be mentioned in one place within the docs. At the moment I’m just trying to discover them for my self.

Note:

The undocumented multi-level controllers feature is reported here.

This issue was discussed as a proposal.

One Response to “ZF Multi-Level Controller Directory - Action controller name formats; keyname, url segment, folder, file, class name”

  1. » Keeping Admin and Public Areas Separate Zend Framework - MVC - ACL versus .htaccess - Eat My Business Says:

    […] have done some testing to investigate possibilities of have a subfolder in my controllers directory for […]

Leave a Reply