Search code examples
phpzend-frameworkzend-framework2zend-config

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.


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?


Solution

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