在 Zend Framework 2 中隐藏模块名称


Hiding Module name in Zend Framework 2

我刚刚从Zend Skeleton Application创建了一个ZF2应用程序。框架的默认路由需要 URL 中的模块名称。例如,要在应用程序模块的索引控制器中调用索引操作,我必须键入 url http://localhost/application/index/index .

我想

从 url 中省略模块名称(因为我只需要一个模块来完成我想到的),这样如果我输入 http://localhost/index/index ,我会得到相同的结果。

我尝试像这样更改应用程序/配置中的默认 module.config.php 文件 -

<?php
return array(
    'router' => array(
        'routes' => array(
            'home' => array(
                'type' => 'Zend'Mvc'Router'Http'Literal',
                'options' => array(
                    'route'    => '/',
                    'defaults' => array(
                        'controller' => 'Application'Controller'Index',
                        'action'     => 'index',
                    ),
                ),
                'may_terminate' => true,
                'child_routes' => array(
                    'default' => array(
                        'type'    => 'Segment',
                        'options' => array(
                            'route'    => '/[:controller[/:action]]',
                            'constraints' => array(
                                'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                'action'     => '[a-zA-Z][a-zA-Z0-9_-]*',
                            ),
                            'defaults' => array(   
                                'action' => 'index',
                                '__NAMESPACE__' => 'Application'Controller'                             
                            ),
                        ),
                    ),
                ),
            ),            
        ),
    ),
    'service_manager' => array(
        'factories' => array(
            'translator' => 'Zend'I18n'Translator'TranslatorServiceFactory',
        ),
    ),
    'translator' => array(
        'locale' => 'en_US',
        'translation_file_patterns' => array(
            array(
                'type'     => 'gettext',
                'base_dir' => __DIR__ . '/../language',
                'pattern'  => '%s.mo',
            ),
        ),
    ),
    'controllers' => array(
        'invokables' => array(
            'Application'Controller'Index' => 'Application'Controller'IndexController',
            'Application'Controller'Home' => 'Application'Controller'HomeController',
        ),
    ),
    '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__ . '/../view/layout/layout.phtml',
            'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
            'error/404'               => __DIR__ . '/../view/error/404.phtml',
            'error/index'             => __DIR__ . '/../view/error/index.phtml',
        ),
        'template_path_stack' => array(
            __DIR__ . '/../view',
        ),
    ),
);

但是它不起作用。谁能帮我?

尝试从子路由中删除斜杠。也就是说,/[:controller[/:action]]应该是[:controller[/:action]].那么它应该可以工作。

如果您对解决方案的演练感兴趣,几个月前我写了一篇关于它的文章。

我在module.config.php中用这个路由定义做了这个技巧:

'router' => array(
    'routes' => array(
        'home' => array(
            'type' => 'Zend'Mvc'Router'Http'Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    'controller' => 'Application'Controller'Index',
                    'action'     => 'index',
                ),
            ),
        ),
        'application' => array(
            'type' => 'segment',
            'options' => array(
                'route'    => '[/:controller[/:action]][/]',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application'Controller',
                    'controller'    => 'Index',
                    'action'        => 'index',
                ),  
            ),
        ), 
    ),
),

使用此行仅使用控制器和操作名称而不使用模块

'route'    => '[/:controller[/:action]][/]'