Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Default Module in Zend Framework

Does anyone know of a way to set the default module dynamically in Zend Framework and not run into namespace issues? For example, what I want to do is have a table of modules that are allowed to be loaded, with one of them set as the default module. For example, I may have:

admin
blog
calendar

as modules that can be loaded. If I have 'blog' as the default module, then 'admin' and 'calendar' have to have their controllers namespaced (Admin_IndexController, Calendar_IndexController) while 'blog' isn't (IndexController).

If I change 'calendar' to be the default module, ZF can no longer find the classes because of the namespacing.

How do you get around that? I'm currently using the following code:

$modules = new Modules();
$activeModules = $modules->fetchActive();
foreach($activeModules as $mod) {
    $loadedModules[$mod->name] = '..application/modules/' . $mod->name . '/controllers';
    if($mod->default) {
        $defaultModule = $mod->name;
    }
}
$frontController->setControllerDirectory($loadedModules);
$frontController->setDefaultModule($defaultModule);
like image 822
dragonmantank Avatar asked May 17 '09 23:05

dragonmantank


3 Answers

If you plan on changing the default module, it is probably best to namespace ALL modules, and then specify that the default module should be prefixed:

First change the "blog" module to use namespacing:

<?php

// Used to be "class IndexController"
class Blog_IndexController extends Zend_Controller_Action {

}

Then, call setParam for prefixDefaultModule option on your instance of Zend_Controller_Front:

<?php

    // Allow your default module to be prefixed
    $frontController->setParam('prefixDefaultModule', true);

See bug # 1831 for an explanation.

like image 133
Jake McGraw Avatar answered Nov 20 '22 08:11

Jake McGraw


use application.ini: resources.frontController.prefixDefaultModule = true resources.frontController.defaultModule = default

like image 4
Sel Avatar answered Nov 20 '22 06:11

Sel


You can make a default module actually be the deciding part in this whole process. More specifically - make all requests for default module go to a class that then will decide what specific module is currently default one and will re-route request to it.

At least that's the way we've implemented it ;)

like image 1
xelurg Avatar answered Nov 20 '22 07:11

xelurg