Get list of all modules with all controllers and actions in Zend Framework
by Raza on Jan.06, 2011, under Zend Framework
Hi guys!
Finally back after a long time. I have been really busy at work. In one of the projects, i had to get list of modules plus controllers and their respective actions for ACL configuration purposes .. I implemented the said task using action helper and controller plugin in Zend Framework. I am listing the Action helper code here. Also, when done with ACL, i will post that code here as well …
Before getting started, i am going to assume that you have configured Zend Framework installation on your local machine and setup your application through a virtual host.
First, open the Bootstrap.php in your application directory and put the following code:
<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoLoad(){
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('Application_');
return $autoLoader;
}
protected function _initActionHelpers() {
Zend_Controller_Action_HelperBroker::addHelper(new Application_Action_Helper_AssetsList());
}
Now do the following:
- Create a folder called Application in your site’s library folder.
- Then create sub-folder called Actionin the Applicationfolder.
- Again create a sub-folder called Helperin the Actionfolder.
- Now a create a file called AssetsList.phpin the plugin folder.
In the AssetsList.php folder, write the following code:
<?php
class Application_Action_Helper_AssetsList extends Zend_Controller_Action_Helper_Abstract {
public function direct() {
}
public function getList() {
$module_dir = $this->getFrontController()->getControllerDirectory();
$resources = array();
foreach($module_dir as $dir=>$dirpath) {
$diritem = new DirectoryIterator($dirpath);
foreach($diritem as $item) {
if($item->isFile()) {
if(strstr($item->getFilename(),'Controller.php')!=FALSE) {
include_once $dirpath.'/'.$item->getFilename();
}
}
}
foreach(get_declared_classes() as $class){
if(is_subclass_of($class, 'Zend_Controller_Action')) {
$functions = array();
foreach(get_class_methods($class) as $method) {
if(strstr($method, 'Action')!=false) {
array_push($functions,substr($method,0,strpos($method,"Action")));
}
}
$c = strtolower(substr($class,0,strpos($class,"Controller")));
$resources[$dir][$c] = $functions;
}
}
}
return $resources;
}
}
To get the list, simply put the following line in any controller action:
$resources = $this->_helper->AssetsList->getList();
Hopefully, this helps out everyone. I will post the dynamic ACL plugin code in coming days when i am finished with it.
-
max4ever
-
http://Problemaboutit, Dr.SoFtNaF
-
http://www.geekpub.de Arne Riemann
-
http://twitter.com/Tzonev Martin Tzonev
-
http://www.facebook.com/vitorgabriel11 Gabriel Vítor de Sousa







