简体   繁体   English

防止合并Zend Framework 2中的两个模块配置

[英]prevent merging two module config in Zend Framework 2

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. 我的ZF2应用程序中有两个模块,这两个模块各自具有不同的配置,并且两个模块都具有不同的Module.php,并且内部具有不同的配置。


I have a login process for Admin, which is defined in Module.php like below: in onBootstrap funtion: 我有一个用于Admin的登录过程,该过程在Module.php中定义如下:onBootstrap函数中:

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 function which is called inside the onBootstrap for login process check boforeDispatch函数,该函数在onBootstrap内部onBootstrap以进行登录过程检查

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

Whenever I am going to run Front module, Admin module's function beforeDispatch is running. 每当我要运行Front模块时,运行Disdisatch之前Admin模块的功能。 I also tried to define another function inside Front module with no content inside so that it could not merge it. 我还尝试在Front模块内部定义另一个函数,该函数内部没有内容,因此无法合并它。


2 2

I have written different 404 template for both module, but Front's template is running. 我为两个模块编写了不同的404模板,但是Front的模板正在运行。 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? 问:如何防止将一个模块配置与另一个模块合并?

You shouldn't prevent merging. 您不应该阻止合并。 There is a similar problem with loading different layout for 2 modules - take a look https://stackoverflow.com/a/11921330/949273 为2个模块加载不同的布局也存在类似的问题-看看https://stackoverflow.com/a/11921330/949273

Unfortunately, Your issue is a bit contradictory because if you got a 404 page, there is no way to know what module is that - because of that it's called 404 page not found . 不幸的是,您的问题有点矛盾,因为如果您有404页面,则无法知道该模块是什么模块-因此,它被称为404页面未找到

Anyway you can dispatch MvcEvent::EVENT_DISPATCH_ERROR event and check with regular expression URL and set different view file. 无论如何,您可以调度MvcEvent :: EVENT_DISPATCH_ERROR事件并使用正则表达式URL进行检查并设置其他视图文件。

Code example 代码示例

in your admin module config 在您的管理模块配置中

'template_map' => array(
    'error-admin/404' => __DIR__ . '/../view/error/404.phtml',
),

than on EVENT_DISPATCH_ERROR inject your logic 比在EVENT_DISPATCH_ERROR上注入您的逻辑

public function onBootstrap(MvcEvent $e)
{
    $app = $e->getTarget();
    $em  = $app->getEventManager();
    $em->attach(MvcEvent::EVENT_DISPATCH_ERROR, function (\Zend\Mvc\MvcEvent $e) {
        $app = $e->getParam('application');
        $uri = $app->getRequest()->getUri()->getPath();

        if(strpos($uri, '/admin') === 0){
            $view = new \Zend\View\Model\ViewModel();
            $view->setTemplate('error-admin/404');
            $e->setViewModel($view);
        }
    });
}

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() 我在MvcEvent::getRouteMatch()->getParam()的帮助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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM