Admin area and module activation – Part 1

by Iva Korlevic
May 16, 2013

Hello,
I recently needed to make module activation in the admin area of our online booking system that we’re developing. Here is how I solved it.

Our online booking system is based on Zend framework, and for now doesn’t deviate much from Zend’s project structure. Our modules are situated in application/modules folder. We’ve already made admin area as a module when we started to work on incorporating other modules with our system. And now the question was asked – how do we enable and disable modules from our admin area? Our admin area is already a module itself so we cannot make new modules as a part of our admin module. New module itself also has to show certain information (forms) on the frontend and backend, how to include it to do exactly what we need? I then stumbled on the solution to make admin area as admin controller but discarded it after some thought. We already have working admin area of our system so why should we change it? Do all the work again and then come to basically almost the same point. So I played a bit with various versions of module activation and files, and came up with this idea.

I’m going to make a config file that has some basic information about the module that is going to be filled during the installation process of the module itself. At that time I filled it by hand, and I oped for .ini type of file. But you can easly make it as a yaml or xml file. The following code is situated in modules.ini file in application/modules/config folder. My booking module is situated in application/modules/local folder.
Basic info in .ini file is consisted of module folder name, module name and is module active info. Here’s how my basic info looks like:

<code class="language-php">// this code goes into modules.ini file
[booking]                                           // marks new part of modules.ini file, same as module folder name
module.name = "Booking"
module.active = "1"
</code>

Now that was the easy part 🙂 . Let’s get on to the hard part.
We want our application to show enabled and not show disabled modules, so we need to write something in our bootstrap file. I presume you know how to detect modules in /modules folder using your application.ini file. Since our module is situated in /local folder inside the /modules folder we have to designate that folder also as our modules folder. First we’ll make public function _initMods() in our Bootstrap.php file. Since we’re going to need some things for module activation we’re going to put them at the beginning. We’re going to instance our front controller for later use and then ‘read’ the names from /local folder.

<code class="language-php">// this code goes into application/Bootstrap.php file
public function _initMods(){
        
        $config = new Zend_Config_Ini(APPLICATION_PATH. '/modules/config/modules.ini');  // new object of Zend_Config_Ini, 
        //we use it to read our modules.ini file
        
        $directories = glob(APPLICATION_PATH .'/modules/local/*' , GLOB_ONLYDIR); //grab our module directories from /modules/local folder
        $directories = str_replace(APPLICATION_PATH .'/modules/local/','', $directories); // we'll need only names, not paths
        
        $this->bootstrap('FrontController');                      // we need FrontController to add new module directory
        $frontctrl = $this->getResource('FrontController');       //  get FrontController and all of its current properties
}
</code>

now we have everything we need to add new module. We bootstrap the FrontController in order to get everything that is currently set in it. This proved to be crutial for adding new modules folder. Here’s the code for adding new active module. I’ll explain it in comments and further bellow:

<code class="language-php">// this code goes into application/Bootstrap.php file
public function _initMods(){
        
// .....
foreach($directories as $name){
        	
        	if($name != "admin" &&  $name != "local" && $name != "config" ){ // we don't need to activate these modules
        		
        		$active = $config->$name->module->active;            //here we get the module.active = ? value
        			if($active){                                 // if module.active = 1 
        			$frontctrl->addModuleDirectory(APPLICATION_PATH .'/modules');
        			$frontctrl->addControllerDirectory(APPLICATION_PATH .'/modules/local/'. $name  .'/controllers',$name);     // add module directory into the list of controler directories
        			
// in order for our module to be fully functional we have to bootstrap it's Bootstrap.php file. Only then our module
// is seen as a module by Zend Framework
        			require_once APPLICATION_PATH .'/modules/local/'. $name .'/Bootstrap.php';  // get the contents of our modules's Bootstrap.php
        			$className = ucfirst($name) .'_Bootstrap'; 
        			$moduleBootstrap = new $className($this);   // make new instance of our module's Bootstrap.php
        			$moduleBootstrap->bootstrap();              // bootstrap our module's Bootstrap.php
        				
        				
        			}
        		}
        }

}
</code>

And now we have initialized our active modules. If we try to access the modules with module.active=0 we’ll get an error. If a module has module.active=1 it is initialized and we can access our modules functions and controllers.
For the next part I’ll show you how to make activation from admin area.

Cheers! 🙂

Comments

0 thoughts on “Admin area and module activation – Part 1”

Tell us your opinion:

Your email address will not be published. Required fields are marked *