ZF2新模块中断了路由

时间:2013-04-04 13:57:47

标签: php zend-framework2

我有一个奇怪的问题。如果我有1个模块,我的路由正确匹配并返回页面。将另一个模块添加到应用程序配置和BANG - 它停止工作并最终在我的第二个模块的Module.php中(它具有boostrapping并触发大量事件)。

application.config.php

<?php
return array(
    'modules' => array(        
        'CMS',
        'AOCS',
    ),
    'module_listener_options' => array(
        'config_glob_paths'    => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
        'module_paths' => array(
            './module',
            './vendor',
        ),
    ),
);

CMS模块配置

<?php
return array(
    'router' => array(
        'routes' => array(                   
            'Admin' => array(        
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/admin',
                    'defaults' => array(
                        '__NAMESPACE__' => 'CMS\Controller',
                        'controller'    => 'Index',
                        'action'        => 'Login',
                    ),
                ),
                'may_terminate' => true,
            ),
            'Logout' => array(        
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/logout',
                    'defaults' => array(
                        '__NAMESPACE__' => 'CMS\Controller',
                        'controller'    => 'Index',
                        'action'        => 'Logout',
                    ),
                ),
            ),                                    
            'CMS/Welcome' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/aocs/welcome',
                    'defaults' => array(
                        '__NAMESPACE__' => 'AOCS\Controller',
                        'controller'    => 'Welcome',
                        'action'        => 'Welcome',
                    ),
                ),                
            ),                      
        ),
    ),   
    'controllers' => array(
        'invokables' => array(
            'CMS\Controller\Index' => 'CMS\Controller\IndexController',
        ),
    ),
    'translator' => array(
        'locale' => 'en_GB',        
    ),    
    '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/layout'           => __DIR__ . '/../templates/admin.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',          
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),      
    ),
);

AOCS模块配置

<?php
return array(
    'router' => array(
        'routes' => array(                                         
            'Welcome' => array(
                'type'    => 'Literal',
                'options' => array(
                    'route'    => '/welcome',
                    'defaults' => array(
                        '__NAMESPACE__' => 'AOCS\Controller',
                        'controller'    => 'Welcome',
                        'action'        => 'Welcome',
                    ),
                ),                
            ),            
            'Mains' => array(
                'type'    => 'segment',
                'options' => array(
                    'route'    => '/aocs[/:controller][/:action][/:id]',
                    'defaults' => array(
                        '__NAMESPACE__' => 'AOCS\Controller',
                        'controller'    => 'Welcome',
                        'action'        => 'Welcome',
                    ),                   
                ),
                'may_terminate' => true,
                'child_routes'  => array(
                    'query' => array(
                        'type' => 'Query',
                    ),
                ),                                               
            ),            

        ),
    ),
    'navigation' => array(
        'menu' => array(
            'page-1' => array(
                'label' => 'Logout',
                'route' => 'Logout',
                'resource' => 'aocs_index_logout'
            ),
            'page-2' => array(
                'label' => 'Login',
                'route' => 'Admin' ,
                'resource' => 'cms_index_login'
            ),
        ),
    ),    
    'service_manager' => array(
        'factories' => array(
          'menu' => 'AOCS\Navigation\MenuNavigationFactory'
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'AOCS\Controller\Welcome' => 'AOCS\Controller\WelcomeController'
        ),
    ),
    'translator' => array(
        'locale' => 'en_GB',        
    ),    
    '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/layout'           => __DIR__ . '/../templates/admin.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',          
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
        /*'strategies' => array(
            'ViewJsonStrategy',
        ),*/        
    ),
);
来自CMS的

Module.php

<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace CMS;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;   
use Zend\View\Renderer\JsonRenderer;
use Zend\Di\Di;        
use Zend\Config\Reader\Ini;
use Zend\Navigation\Navigation;

// AH core code
use Core\Classes\setDB;
use Core\Classes\setCache;
use Core\Models\SecurityModel;
use Core\Models\AclModel;

class Module
{   
    public function onBootstrap(MvcEvent $e)
    {
        $sm = $e->getApplication()->getServiceManager();

        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);       

        // Set config - required throughout
        $reader = new Ini;
        $sm->setService('configIni',$reader->fromFile('./config/application.ini'));        

        // Setup a Database connection
        $sm->setService('setDB',new setDB($sm,'database')); 

        // Setup Caching
        $sm->setService('setCache',new setCache($sm));

        /*$eventManager->attach('dispatch', function ($sm) use ($controllers) {
          print'<pre>[CMS]';print_r($sm->getRouteMatch());print'</pre>';
          exit;         
        }, 100); // execute before executing action logic*/         

        // Assign system names to view models so we can set across templates
        $view = $e->getViewModel();
        $config = $sm->get('configIni');
        $view->setVariable('systemname',$config['system']['name']);
        $view->setVariable('systemshort',$config['system']['shortname']);                             
    }    

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }
}

我错过了一些明显的东西吗?它真的很烦人!

由于

安东尼

1 个答案:

答案 0 :(得分:0)

修正了它!

BANG是我在2x Modules.php中发送的,由于某种原因,它自动触发。当第二个检查用户是否登录并且它们没有被重定向导致问题。

问题与我的配置无关(除非我错过了明显的)这是ZF2中的一个错误,其中调度事件与他们调用的模块无关,但所有运行都被运行自动。

因此,如果我的页面在CMS模块中,AOCS模块&gt; bootstrap仍在运行。为什么?

感谢关于type = Query的观点 - 将会更新。