2中的两个模块配置

2中的两个模块配置

本文介绍了防止合并Zend Framework 2中的两个模块配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ZF2应用程序中有两个模块,两个模块本身都有不同的配置,并且两个模块都有不同的Module.php,其中的配置不同.

I have two module in my ZF2 application, both module have different configuration for themself, and both module have different Module.php with different configruation inside it.

我有一个用于Admin的登录过程,该过程在Module.php中定义,如下所示:在onBootstrap功能中:

I have a login process for Admin, which is defined in Module.php like below:in onBootstrap funtion:

public function onBootstrap($e) {
        $e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e) {
            $controller = $e->getTarget();
            $controllerClass = get_class($controller);
            $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
            if ('Admin' === $moduleNamespace) {
                $controller->layout('layout/admin');
            }
        }, 100);

        $application = $e->getApplication();
        $eventManager = $application->getEventManager();
        ..........
        ..........
        $eventManager->attach(MvcEvent::EVENT_DISPATCH, array($this, 'boforeDispatch'), 100);
    }

boforeDispatch函数,在onBootstrap内部调用以进行登录过程检查

boforeDispatch function which is called inside the onBootstrap for login process check

function boforeDispatch(MvcEvent $event) {
    ......
    //did something
    ......
}


每当我要运行Front模块时,运行Dispatch之前Admin模块的功能.我还尝试在Front模块内部定义另一个函数,该函数内部没有内容,因此无法合并它.


Whenever I am going to run Front module, Admin module's function beforeDispatch is running. I also tried to define another function inside Front module with no content inside so that it could not merge it.

我为两个模块编写了不同的404模板,但是Front的模板正在运行.这是代码.

I have written different 404 template for both module, but Front's template is running. Here is the code.:

'view_manager' => array(
        'display_not_found_reason' => true,
        'display_exceptions'       => true,
        'doctype'                  => 'HTML5',
        'not_found_template'       => 'error/404',
        'exception_template'       => 'error/index',
        'template_map' => array(
            'layout/front'           => __DIR__ . '/../view/layout/layout.phtml',
            'front/index/index' => __DIR__ . '/../view/front/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),

两个文件都位于具有相同结构的模块文件夹中.问:如何防止将一个模块配置与另一个模块合并?

both's files are inside its module folder with same structure.Q: How to prevent merging one module configuration from another?

推荐答案

经过大量搜索,我得到了问题的解决方案.主要问题是获取模块名称.这是我的代码.我在MvcEvent::getRouteMatch()->getParam()

After lots of search I got the solution of my question.Main problem was getting the Module name. here is my code.I generated it with the help of MvcEvent::getRouteMatch()->getParam()

function boforeDispatch(MvcEvent $event) {
    $controller = $event->getRouteMatch()->getParam('controller');
    $request_module = substr($controller, 0, strpos($controller, '\\'));
    if ($request_module == __NAMESPACE__) {
        //do stuff
    }
}

这篇关于防止合并Zend Framework 2中的两个模块配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 22:00