【问题标题】:ZF2 Route To Controller at Top LevelZF2 到顶层控制器的路由
【发布时间】:2014-03-25 00:27:50
【问题描述】:

请原谅我对 ZF2 的新手。 我想从顶层路由到控制器...

在 ZF2 Skeleton 中,它的设置是这样的,这样路由在应用之后被分段:
http://www.example.com/application[/:controller[/:action]]


但我不想像这样进入应用程序路径并进入控制器:
http://www.example.com/[:controller[/:action]]

我进行了广泛的搜索,但无法让它发挥作用。 我的 module.config.php 设置如下:

/* in module.config.php */

'router' => array(
    'routes' => array(
        'home' => array(
            'type'    => 'Zend\Mvc\Router\Http\Literal',
            'options' => array(
                'route'    => '/',
                'defaults' => array(
                    '__NAMESPACE__' => 'Application\Controller',
                    '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(
                        ),
                    ),
                ),
            ),
        ),
    ),
),


'controllers' => array(
    'invokables' => array(
        'Application\Controller\Index' => 'Application\Controller\IndexController',
        'Application\Controller\Login' => 'Application\Controller\LoginController',
    ),
),

如果我浏览到 http://www.example.com/login ,它会显示“请求的 URL 无法通过路由匹配。”我想我缺少基本概念... =(

【问题讨论】:

    标签: routes zend-framework2 segment


    【解决方案1】:

    子路由连接到父路由。这意味着最终您要定义以下可能的路线:

    http://example.com//:controller/:action
    

    注意双斜线。如果您的所有子路由参数都是可选的并且命名空间没有改变,那么将它们定义为 child_route 根本没有意义,最终只会让它变慢。

    【讨论】:

      【解决方案2】:

      在 Sam 的指导下,我能够弄清楚:

      'router' => array(
          'routes' => array(
              'home' => 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(
                          '__NAMESPACE__' => 'Application\Controller',
                          'controller' => 'Index',
                          'action'     => 'index',
                      ),
                  ),
              ),
          ),
      ),
      

      【讨论】:

      • 你需要接受 Sam 的回答,而不是自己写
      • @djay 实际上,只支持我并接受他自己的答案是完全有效的,因为我只是给他一些指标,他用来自己创建正确的答案。
      • 一切都好。谢谢山姆。我真的很喜欢你的博客内容 =)
      猜你喜欢
      • 2016-05-05
      • 2015-02-07
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 2015-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多