簡體   English   中英

防止合並Zend Framework 2中的兩個模塊配置

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

我的ZF2應用程序中有兩個模塊,這兩個模塊各自具有不同的配置,並且兩個模塊都具有不同的Module.php,並且內部具有不同的配置。


我有一個用於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函數,該函數在onBootstrap內部onBootstrap以進行登錄過程檢查

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

每當我要運行Front模塊時,運行Disdisatch之前Admin模塊的功能。 我還嘗試在Front模塊內部定義另一個函數,該函數內部沒有內容,因此無法合並它。


2

我為兩個模塊編寫了不同的404模板,但是Front的模板正在運行。 代碼如下:

'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',
        ),
    ),

這兩個文件都位於具有相同結構的模塊文件夾中。 問:如何防止將一個模塊配置與另一個模塊合並?

您不應該阻止合並。 為2個模塊加載不同的布局也存在類似的問題-看看https://stackoverflow.com/a/11921330/949273

不幸的是,您的問題有點矛盾,因為如果您有404頁面,則無法知道該模塊是什么模塊-因此,它被稱為404頁面未找到

無論如何,您可以調度MvcEvent :: EVENT_DISPATCH_ERROR事件並使用正則表達式URL進行檢查並設置其他視圖文件。

代碼示例

在您的管理模塊配置中

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

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

經過大量搜索,我得到了我問題的解決方案。 主要問題是獲取模塊名稱。 這是我的代碼。 我在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