Home » Php » php – Dynamic Default Module in Zend Framework

php – Dynamic Default Module in Zend Framework

Posted by: admin July 12, 2020 Leave a comment


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:


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;
How to&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:


// 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:


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

See bug # 1831 for an explanation.


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


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 😉


Sounds like the work of a preDispatch Controller Plugin.

You can modify the request to change a module based on certain request or identity/session/known data to forward or redirect on demand.