带有德语特殊字符的 URI 在 Zend Framework 2 中不起作用(错误 404) [英] URIs with german special characters don't work (error 404) in Zend Framework 2
问题描述
我想获得一个城市列表,其中每个城市名称都被链接并引用该城市的一个页面:
I want to get a list of cities, where each city name is linked and refers a page for this city:
链接(在视图脚本中创建)如下所示:
The links (created in the view script) look like this:
http://project.loc/catalog/Berlin (in the HTML source code url-encoded: Berlin)
http://project.loc/catalog/Erlangen (in the HTML source code url-encoded: Erlangen)
http://project.loc/catalog/Nürnberg (in the HTML source code url-encoded: N%C3%BCrnberg)
"Berlin", "Erlangen" 等工作,但如果城市名称包含德语特殊字符 (ä
, ö
, ü
>、Ä
、Ö
、Ü
或 ß
) 像纽伦堡"一样,会出现 404 错误:
"Berlin", "Erlangen" etc. work, but if the city name contains a german special character (ä
, ö
, ü
, Ä
, Ö
, Ü
, or ß
) like "Nürnberg", a 404 error occurs:
发生 404 错误页面未找到.请求的 URL 不能为通过路由匹配.没有可用的异常
A 404 error occurred Page not found. The requested URL could not be matched by routing. No Exception available
为什么?以及如何让它发挥作用?
Why? And how to get this working?
提前致谢!
我的路由器设置:
'router' => array(
'routes' => array(
'catalog' => array(
'type' => 'literal',
'options' => array(
'route' => '/catalog',
'defaults' => array(
'controller' => 'Catalog\Controller\Catalog',
'action' => 'list-cities',
),
),
'may_terminate' => true,
'child_routes' => array(
'city' => array(
'type' => 'segment',
'options' => array(
'route' => '/:city',
'constraints' => array(
'city' => '[a-zA-ZäöüÄÖÜß0-9_-]*',
),
'defaults' => array(
'controller' => 'Catalog\Controller\Catalog',
'action' => 'list-sports',
),
),
'may_terminate' => true,
'child_routes' => array(
// ...
),
),
),
),
),
),
推荐答案
您需要更改约束,您可以使用匹配 UTF8 字符的正则表达式,例如:
You need to change your constraints, you can use a regular expression which will match UTF8 characters, something like this:
'/[\p{L}]+/u'
注意/u 修饰符(unicode).
Notice the /u modifier (unicode).
问题已解决.
说明:
RegEx 路由将 URI 与 preg_match(...)
(行 116 或 118 的 Zend\Mvc\Router\Http\Regex).为了处理带有特殊字符"(128+)的字符串,必须将模式修饰符 u
传递给 preg_match(...)
.像这样:
The RegEx Route maches the URIs with preg_match(...)
(line 116 or 118 of Zend\Mvc\Router\Http\Regex). In order to mach a string with "special chars" (128+) one must pass the pattern modifier u
to preg_match(...)
. Like this:
$thisRegex = '/catalog/(?<city>[\p{L}]*)';
$regexStr = '(^' . $thisRegex . '$)u'; // <-- here
$path = '/catalog/Nürnberg';
$matches = array();
preg_match($regexStr, $path, $matches);
并且由于 RegEx Route 将 url 编码的字符串传递给 preg_match(...)
,因此需要先对字符串进行进一步解码:
And since RegEx Route passes a url-enccoded string to preg_match(...)
, it's furthermode needed to decode the string first:
$thisRegex = '/catalog/(?<city>[\p{L}]*)';
$regexStr = '(^' . $thisRegex . '$)u';
$path = rawurldecode('/catalog/N%C3%BCrnberg');
$matches = array();
preg_match($regexStr, $path, $matches);
RegEx Route 中没有提供这两个步骤,所以 preg_match(...)
得到了类似 '/catalog/N%C3%BCrnberg'
并尝试将其转换为像 '/catalog/(?<city>[\\p{L}]*)/u'
These two steps are not provided in the RegEx Route, so that preg_match(...)
gets a steing like '/catalog/N%C3%BCrnberg'
and tries to mach it to a regex like '/catalog/(?<city>[\\p{L}]*)/u'
解决方案是使用自定义 RegEx 路由.这里是一个例子.
The solution is to use a custom RegEx Route. Here is an example.
这篇关于带有德语特殊字符的 URI 在 Zend Framework 2 中不起作用(错误 404)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!