【发布时间】:2013-03-16 02:54:24
【问题描述】:
我想获取一个城市列表,其中每个城市名称都被链接并引用该城市的页面:
链接(在视图脚本中创建)如下所示:
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 错误:
发生 404 错误页面未找到。请求的 URL 不能是 通过路由匹配。没有可用的例外
为什么?以及如何让它发挥作用?
提前致谢!
编辑:
我的路由器设置:
'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(
// ...
),
),
),
),
),
),
【问题讨论】:
-
这与你定义的
constraints不匹配不是很明显吗? -
是的,你是对的。我刚刚编辑了路由器设置。它仍然无法正常工作。
-
您展示了 URL 的“外观”——但是您是否为这些特殊字符使用了正确的 URL 编码?
-
不,我没有。我将城市名称设置为 URI,以便它们如何来自数据库(
$city->name而不是urlencode($city->name))。 -
如果您自己不对 URL 中的特殊字符进行 urlencode,则将其留给客户端 - 这样您很容易遇到使用的字符编码问题。
标签: uri url-routing zend-framework2 zend-framework-routing url-helper