在Segment类上具有子路由的ZF3可选参数不想匹配

时间:2017-04-18 19:55:38

标签: php zend-framework3

我想创建带有可选[lang]参数的路由,该参数将用于设置应用程序语言,如下所示:

默认lang param示例(例如EN)

domain.tld/
domain.tld/users
domain.tld/contacts
etc.

新lang param(DE for examle)的例子

domain.tld/de
domain.tld/de/users
domain.tld/de/contacts
etc.

这是我的路线配置:

'router' => [
    'routes' => [
        'site' => [
            'type' => Segment::class,
            'options' => [
                'route' => '[/:lang]',
                'constraints' => [
                    'lang' => '[a-z]{2}',
                ],
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action' => 'index',
                    'lang' => 'en',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'home' => [
                    'type' => Literal::class,
                    'options' => [
                        'route' => '/',
                        'defaults' => [
                            'controller' => Controller\IndexController::class,
                            'action' => 'index',
                        ],
                    ],
                    'may_terminate' => true,
                ],
                'application' => [
                    'type' => Segment::class,
                    'options' => [
                        'route' => '/application[/:action]',
                        'defaults' => [
                            'controller' => Controller\IndexController::class,
                            'action' => 'index',
                        ],
                    ],
                    'may_terminate' => true,
                ],
            ],
        ],
    ],
]

当我在网址中有lang param时,一切都很好。但是当我试图打开"默认" lang没有指定它进入路径(例如example.tld / application / test)它给我404错误。

我发现从Segment类转换后的最终正则表达式为(\G(?:/(?P<param1>([a-z]{2})))?),路径为/application/test。在Segment.php:385上执行preg_match时,它返回与以下值匹配的内容:

[
    '0' => '/ad',
    'param1' => 'ad',
    '1' => 'ad',
],

这显然是错误的行为。我的语言设置为&#34; ad&#34;而不是打开应用程序/测试操作。我测试了大约10个正则表达式但没有成功......(例如^[a-z]{2}$)。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您收到404因为网址与site路由不匹配,因此会跳过site的子路由。
试试这个:

        'home' => [
            'type' => Segment::class,
            'options' => [
                'route' => '/[:lang/]',
                'constraints' => [
                    'lang' => '(en|de)?',
                ],
                'defaults' => [
                    'controller' => Controller\IndexController::class,
                    'action' => 'index',
                    'lang' => 'en',
                ],
            ],
            'may_terminate' => true,
            'child_routes' => [
                'application' => [
                    'type' => Segment::class,
                    'options' => [
                        'route' => 'application[/:action]',
                        'defaults' => [
                            'controller' => Controller\IndexController::class,
                            'action' => 'index',
                        ],
                    ],
                ],
            ],
        ]

如果您不提供lang param,则默认情况下(我的示例中为en)参数。
示例:

  

将example.tld /应用/测试

  • langen
  • controllerIndexController
  • actiontesttestAction()
  

将example.tld / DE /应用/测试

  • langde
  • controllerIndexController
  • actiontesttestAction()
相关问题