正则表达式约束以匹配 zend 路由中的 md5 哈希

regex contraint to match md5 hash in zend routing

下面是我模块中定义的路由。

        'download' => array(
            'type'      => 'Segment',
            'options'   => array(
                'route' => '/download[/:transferId][/:receiverId]',
                'constraints' =>array(
                    'transferId' => '/^[a-f0-9]{32}$/i',
                    'receiverId' => '/^[a-f0-9]{32}$/i'
                ),
                'defaults' => array(
                    'controller'    => 'FileServer\Controller\Web',
                    'action'        => 'download',
                )
            )
        ),

这些 url 应该匹配 http://localhost/download/229def85ea0ccfcd6809053cb8fc4911 而这个 http://localhost/download/229def85ea0ccfcd6809053cb8fc4911/229def85ea0ccfcd6809053cb8fc4911 但是 none 匹配

除了约束中的这个正则表达式 /^[a-f0-9]{32}$/i 我也尝试了这些但它不起作用

^[a-fA-F0-9]{32}$
[a-fA-F0-9]{32}
[a-f0-9]{32}

怎么了?

Apart from this regex in the constraint /^[a-f0-9]{32}$/i I tried these as well but its not working

^[a-fA-F0-9]{32}$ [a-fA-F0-9]{32}

这不起作用,因为 [a-f0-9]{32} 仅匹配字母或数字 32 次,而在您的字符串中,有一个分号 : 和一个正斜杠 / .

你可以利用这个模式

http:\/\/\S+

这严格确保 http:// 出现在比赛中

Demo

如果您想要更通用的模式,请使用此

[a-z]+:\/\/\S+

Demo

你可能想要:

'constraints' => array(
    'transferId' => '[a-f0-9]{32}',
    'receiverId' => '[a-f0-9]{32}'
),

(您不包括正则表达式的其他部分,因为 ZF2 将所有约束组合到一个正则表达式模式中。)

有关更多示例,请参阅文档:http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html#zend-mvc-router-http-segment