I use the following method:
Using conventional modular directory layout most controllers go into a 
folder/directory named after the module.

controllers/admin    
                /testing
                /etc
                    
Controllers in the testing module might be cookies, pages and so on:

controllers/testing/CookiesController.php 
controllers/testing/PagesController.php 

Now, I create a subfolder in the testing folder named after the controller
controllers/testing
               /pages
               /cookies

I factor common utility functions used by the PagesController actions into a 
file called pages_utilities.php. 
The file page_utilities.php goes into the /pages subfolder, so we have:

controllers/testing/CookiesController.php   // controller action scripts
controllers/testing/PagesController.php                   
controllers/testing                                    // sub directory 
               /pages/pages_utilities.php         // file containing page 
controller utility functions
               /pages/enroll_form.php              // contains a function that 
returns a form used by the page controller
controllers/cookies                                  // same as above
               /cookies_utilities.php
               /cookies/test_form.php

Inside the PagesController.php require_once allows access to controller 
specific files

require_once 'pages/pages_utilities.php';
require_once 'pages/enroll_form.php';
class Testing_PagesController{...}

This allows me to move the controller around to different modules as long as I 
move its subdir along with it.
(and change the class name of course, Testing_PagesController would become 
ModuleX_PagesController).

I should point that with this method you need to be on the alert for link rot 
especially in forms.  
If your form posts to /testing/pages/add  and you move the controller to a new 
module named 'working',
you should change your form to post to /working/pages/add.

This leads to module relative addressing stuff like:

$view->menu = 
create_nav_menu(get_menu_items($this->getRequest()->module),'Delete Item');

The create_nav_menu function is in one of the directories in the php include 
path making it available 
to all controllers in all modules.  Its first parameter is a function that 
returns an array of menu items, like:

  get_menu_items($mod) { $items = array ('Add Item'  => "$mod/pages/add",    
'Delete Item'   => "$mod/pages/delete); return $items; }

The second parameter is a menu item so it can treat the selected menu item 
different from the un-selected ones, hilite it maybe.

The function get_menu_items()  is in the file 'pages/pages_utilities.php'  its 
only parameter (look closely) is the module name.
It injects the module name into the above $mod variable

Now, when the controller is moved to a new module the module relative address 
can track automatically.

My controllers are much smaller now that I factor out common or complex 
functions especially forms!

I hope this helps and if I am doing it wrong or backwards,  please let me know.




Pat Veach 

Reply via email to