【问题标题】:Hiding Module name in Zend Framework 2Zend Framework 2 中隐藏模块名称
【发布时间】:2014-04-28 02:18:57
【问题描述】:

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

我想从 url 中省略模块名称(因为我只需要一个模块来满足我的想法)所以如果我输入 http://localhost/index/index,我会得到相同的结果.

我尝试像这样更改 application/config 中的默认 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',
        ),
    ),
);

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

【问题讨论】:

    标签: php zend-framework2


    【解决方案1】:

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

    如果您对解决方案的演练感兴趣,几个月前我写了an article 来讨论它。

    【讨论】:

      【解决方案2】:

      我在 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]][/]'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多