prevent merging two module config in Zend Framework 2

送分小仙女□ 提交于 2020-01-06 03:02:09

问题


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.


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 function which is called inside the onBootstrap for login process check

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

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.


2

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?


回答1:


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

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.

Anyway you can dispatch MvcEvent::EVENT_DISPATCH_ERROR event and check with regular expression URL and set different view file.

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

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



回答2:


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
    }
}


来源:https://stackoverflow.com/questions/34489723/prevent-merging-two-module-config-in-zend-framework-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!