Uri 带有德语特殊字符don';Zend Framework 2中的t工作(错误404)
我想获得一个城市列表,其中每个城市名称都有链接,并引用该城市的一个页面: 链接(在视图脚本中创建)如下所示:Uri 带有德语特殊字符don';Zend Framework 2中的t工作(错误404),uri,url-routing,zend-framework2,zend-framework-routing,url-helper,Uri,Url Routing,Zend Framework2,Zend Framework Routing,Url Helper,我想获得一个城市列表,其中每个城市名称都有链接,并引用该城市的一个页面: 链接(在视图脚本中创建)如下所示: 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ür
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)
'/[\p{L}]+/u'
“Berlin”、“Erlangen”等都可以,但如果城市名称包含一个德语特殊字符(ä
,ö
,Ä
,Ö
,Ü
或ß
)如“Nürnberg”,则会发生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(
// ...
),
),
),
),
),
),
您需要更改约束,您可以使用与UTF8字符匹配的正则表达式,如下所示:
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)
'/[\p{L}]+/u'
请注意/u修饰符(unicode)
编辑:
问题是
说明:
RegEx路由使用preg\u match(…)
(行或Zend\Mvc\Router\Http\RegEx)处理URI。为了加工带有“特殊字符”(128+)的字符串,必须将模式修饰符u
传递到preg\u match(…)
。像这样:
$thisRegex = '/catalog/(?<city>[\p{L}]*)';
$regexStr = '(^' . $thisRegex . '$)u'; // <-- here
$path = '/catalog/Nürnberg';
$matches = array();
preg_match($regexStr, $path, $matches);
正则表达式路由中没有提供这两个步骤,因此preg_match(…)
会获得类似'/catalog/N%C3%BCrnberg'
的steing,并尝试将其转换为类似正则表达式的'/catalog/(?[\\p{L}]*)/u'
解决方案是使用自定义正则表达式路由。就是一个例子。很明显,这与您定义的约束不匹配吗?是的,您是对的。我刚刚编辑了路由器设置。它仍然不起作用。你展示了URL的“外观”——但是你对这个特殊字符使用了正确的URL编码吗?不,我没有。我将城市名称设置为URI,以便它们如何从数据库中提交(
$city->name
,而不是urlencode($city->name)
)。如果您自己不在URL中对特殊字符进行urlencode,那么您可以将其留给客户端,这样您就很容易在使用的字符编码中遇到问题。谢谢!这对我不起作用,但也许我用错了你的正则表达式。我用'city'=>'/^\p{L}[\p{L}.-]+$/u'替换了'city'=>'[a-zA-Zääü196;äääääääää0-9ä-]*,
,也许这是个糟糕的例子,只是为了阐明/u标志。试试“/[\p{L}]+/u”它终于起作用了!感谢您对修改器的如此重要的提示!我已经发布了解释和解决方案的链接,作为您答案的补充。你是什么意思?我把这个添加到我的修饰符中:/[\p{L}]+/u,但仍然不能工作。Automatix,你能分享一下你用过的吗?