Zend2路由像enum这样的约束

时间:2015-07-08 08:26:02

标签: php zend-framework2 constraints zend-route

我希望对传递给我项目路线的变量有一个类似枚举的枚举

e.g。变量routeVar只能是 optin 退出

所以我想说我想要一个像http://subdomain.exampledomain.com/route/这样的网址,它只能获得以下两种形式中的一种

  1. http://subdomain.exampledomain.com/route/的选择启用
  2. http://subdomain.exampledomain.com/route/的选择禁用
  3. 我放在一起的路线配置如下所示,我尝试了[optin|optout][optin|optout]+正则表达但没有运气

    'route-name' => [
        'type' => 'Segment',
        'options' => [
            'route' => '/route/:routeVar',
            'constraints' => [
                'routeVar' => '[optin|optout]',
            ],
            'defaults' => [
                'controller' => someController::class,
                'action' => 'someAction',
            ],
        ],
        'may_terminate' => true,
    ],
    

    请注意:这是一个子路由,它可以解决预期的控制器和操作。我只是没有使用约束来应用我描述的限制:(

1 个答案:

答案 0 :(得分:1)

你正在寻找的正则表达式是

(optin|optout)

[]语法用于字符组。

实际上甚至可能

^(optin|optout)$

确保值中没有多余的字符。