Flexible controller and modelpaths

By Joshua Angnoe (Dyflexis)
This short tutorial will show you how to organize your model and controller in subfolders of the controllers and models folders!
Cake enables you to define paths in the config/bootstrap.php file. If you add some more logic to that file, you will be able to create subfolders in the /controller and /model folders and place some controllers and models there. This could be usefull when baking large cakes and if you want to maintain some form of overview.

Here's how:

in config/bootstrap.php add something like:

Download code
$modelPaths = array();
$controllerPaths = array();

function enableSubFoldersOn($baseDir, &$var) {         
  $cwd =getcwd();
  chdir($baseDir);
  $dirs = glob("*", GLOB_ONLYDIR);
  if(sizeof($dirs) > 0) { 
    foreach($dirs as $dir) { 
      $var[] = $baseDir.DS.$dir.DS;
    }
  }
  chdir($cwd);
}

enableSubFoldersOn(ROOT.DS.APP_DIR.'/controllers', $controllerPaths);
enableSubFoldersOn(ROOT.DS.APP_DIR.'/models', $modelPaths);

Now you can order your controllers and models in subfolders!

I hope this is useful to you!

 

Comments 364

CakePHP Team Comments Author Comments
 

Comment

1 How to use this function

Can you describe how to use this to make Flexible controller and modelpaths

there is good if you can give some example

thank a lot!!
Posted Jun 20, 2007 by Jersus Soo