Dynamic Default Module in Zend Framework

2020-07-10 06:43发布

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);

4条回答
Deceive 欺骗
2楼-- · 2020-07-10 07:19

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 ;)

查看更多
家丑人穷心不美
3楼-- · 2020-07-10 07:24

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.

查看更多
够拽才男人
4楼-- · 2020-07-10 07:36

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

查看更多
beautiful°
5楼-- · 2020-07-10 07:37

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.

查看更多
登录 后发表回答