防止在 Zend Framework 2 中合并两个模块配置
prevent merging two module config in Zend Framework 2
我的 ZF2 应用程序中有两个模块,两个模块都有不同的配置,并且两个模块都有不同的 Module.php,内部配置不同。
我有一个管理员登录过程,它在 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);
}
在 onBootstrap
内部调用的 boforeDispatch 函数用于登录过程检查
function boforeDispatch(MvcEvent $event) {
......
//did something
......
}
每当我要去运行Front模块时,Admin模块的beforeDispatch函数就是运行ning。我还尝试在 Front 模块中定义另一个函数,里面没有内容,因此无法合并它。
2
我为两个模块编写了不同的 404 模板,但 Front 的模板是 运行ning。这是代码。:
'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 个模块加载不同的布局也存在类似的问题 - 看看
不幸的是,你的问题有点矛盾,因为如果你有一个 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()
的帮助下生成了它
function boforeDispatch(MvcEvent $event) {
$controller = $event->getRouteMatch()->getParam('controller');
$request_module = substr($controller, 0, strpos($controller, '\'));
if ($request_module == __NAMESPACE__) {
//do stuff
}
}
我的 ZF2 应用程序中有两个模块,两个模块都有不同的配置,并且两个模块都有不同的 Module.php,内部配置不同。
我有一个管理员登录过程,它在 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);
}
在 onBootstrap
内部调用的 boforeDispatch 函数用于登录过程检查
function boforeDispatch(MvcEvent $event) {
......
//did something
......
}
每当我要去运行Front模块时,Admin模块的beforeDispatch函数就是运行ning。我还尝试在 Front 模块中定义另一个函数,里面没有内容,因此无法合并它。
2
我为两个模块编写了不同的 404 模板,但 Front 的模板是 运行ning。这是代码。:
'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 个模块加载不同的布局也存在类似的问题 - 看看
不幸的是,你的问题有点矛盾,因为如果你有一个 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()
function boforeDispatch(MvcEvent $event) {
$controller = $event->getRouteMatch()->getParam('controller');
$request_module = substr($controller, 0, strpos($controller, '\'));
if ($request_module == __NAMESPACE__) {
//do stuff
}
}