zend framework 2 路由与 UTF-8 正则表达式模式
zend framework 2 Routing with UTF-8 Regex Pattern
我希望我的路由支持波斯字符,但是当在正则表达式路径中使用 x{600}-\x{6FF} 时,应用会给我一个 404 错误(找不到页面)
我的 zf2 路由配置:
'result' => array(
'type' => 'segment',
'options' => array(
'route' => '/Result[/:name]',
'constraints' => array(
'name' => '[\sa-zA-Z0-9_.-x{600}-\x{6FF}]*',
),
'defaults' => array(
'controller' => 'Result',
'action' => 'index',
),
),
),
并且我在 Zend\Mvc\Router\Http\Segment.php
中的正则表达式末尾添加 u
if ($pathOffset !== null) {
$result = preg_match('(\G' . $regex . ')u', $path, $matches,null, $pathOffset);
} else {
$result = preg_match('(^' . $regex . '$)u', $path, $matches);
}
我的 URL 是这样的:
http://localhost/test/Result/A.8.کلمه
您在字符 class 问题中错位了一个连字符,并且在第一个 x{600}
之前缺少了一个 \
。 hyphen changes the meaning of the pattern 是这样的:
要修复它,你需要把它移到字符的末尾class:
'[\sa-zA-Z0-9_.\x{600}-\x{6FF}-]*'
参见 another demo。
请注意,如果您需要向模式中添加更多字符,请注意 [
、]
、-
、/
。必须转义它们以避免出现问题。例如,添加 %
:
'[%\sa-zA-Z0-9_.\x{600}-\x{6FF}-]*'
下一个问题是您需要将 rawurldecode($path)
传递给 preg_match
而不仅仅是 $path
.
我希望我的路由支持波斯字符,但是当在正则表达式路径中使用 x{600}-\x{6FF} 时,应用会给我一个 404 错误(找不到页面)
我的 zf2 路由配置:
'result' => array(
'type' => 'segment',
'options' => array(
'route' => '/Result[/:name]',
'constraints' => array(
'name' => '[\sa-zA-Z0-9_.-x{600}-\x{6FF}]*',
),
'defaults' => array(
'controller' => 'Result',
'action' => 'index',
),
),
),
并且我在 Zend\Mvc\Router\Http\Segment.php
中的正则表达式末尾添加u
if ($pathOffset !== null) {
$result = preg_match('(\G' . $regex . ')u', $path, $matches,null, $pathOffset);
} else {
$result = preg_match('(^' . $regex . '$)u', $path, $matches);
}
我的 URL 是这样的:
http://localhost/test/Result/A.8.کلمه
您在字符 class 问题中错位了一个连字符,并且在第一个 x{600}
之前缺少了一个 \
。 hyphen changes the meaning of the pattern 是这样的:
要修复它,你需要把它移到字符的末尾class:
'[\sa-zA-Z0-9_.\x{600}-\x{6FF}-]*'
参见 another demo。
请注意,如果您需要向模式中添加更多字符,请注意 [
、]
、-
、/
。必须转义它们以避免出现问题。例如,添加 %
:
'[%\sa-zA-Z0-9_.\x{600}-\x{6FF}-]*'
下一个问题是您需要将 rawurldecode($path)
传递给 preg_match
而不仅仅是 $path
.