How to set a (UTF8) modifier for RegEx of a RegEx Route in Zend Framework 2?(如何在 Zend Framework 2 中为 RegEx 路由的 RegEx 设置 (UTF8) 修饰符?)
问题描述
我有 URI 中(德语)特殊字符的问题 并希望尝试使用 RegEx Route 和 PCRE 模式修饰符,用于 UTF-8 u.
I'm having troubles with (german) special characters in URIs and want to try to resolve it with a RegEx Route and a PCRE pattern modifier for UTF-8 u.
'router' => array(
    'routes' => array(
        // ...
        'city' => array(
            'type'  => 'regex',
            'options' => array(
                'regex' => '/catalog/(?<city>[a-zA-Z0-9_-äöüÄÖÜß]*)/u',
                'defaults' => array(
                    'controller' => 'CatalogControllerCatalog',
                    'action'     => 'list-sports',
                ),
                'spec'  => '/catalog/%city%',
            ),
            'may_terminate' => true,
        ),
    ),
),
但是当我设置它时,路由完全停止工作(错误 404)——无论是带有特殊字符的 URI 还是没有特殊字符的 URI.
But when I set it, the route stopps to work at all (error 404) -- neither for URIs with nor to ones without special characters.
如何正确设置修饰符?
推荐答案
因为我已经打开了这个,这里有一个处理程序可以解决这个问题.
Since I already had this open here's a handler that solves the problem.
<?php
namespace ApplicationMvcRouterHttp;
use ZendMvcRouterHttpRegex;
use ZendMvcRouterHttpRouteMatch;
use ZendStdlibRequestInterface as Request;
class UnicodeRegex extends Regex
{
    /**
     * match(): defined by RouteInterface interface.
     *
     * @param  Request $request
     * @param  integer $pathOffset
     * @return RouteMatch
     */
    public function match(Request $request, $pathOffset = null)
    {
        if (!method_exists($request, 'getUri')) {
            return null;
        }
        $uri  = $request->getUri();
        // path decoded before match
        $path = rawurldecode($uri->getPath());
        // regex with u modifier    
        if ($pathOffset !== null) {
            $result = preg_match('(G' . $this->regex . ')u', $path, $matches, null, $pathOffset);
        } else {
            $result = preg_match('(^' . $this->regex . '$)u', $path, $matches);
        }
        if (!$result) {
            return null;
        }
        $matchedLength = strlen($matches[0]);
        foreach ($matches as $key => $value) {
            if (is_numeric($key) || is_int($key) || $value === '') {
                unset($matches[$key]);
            } else {
                $matches[$key] = $value;
            }
        }
        return new RouteMatch(array_merge($this->defaults, $matches), $matchedLength);
    }
}
假设你把文件放在 Application/Mvc/Router/Http/UnicodeRegex 你的路由定义应该是这样的
Assuming you place the file in Application/Mvc/Router/Http/UnicodeRegex your route definition should look like this
'router' => array(
    'routes' => array(
        // ...
        'city' => array(
            'type'  => 'ApplicationMvcRouterHttpUnicodeRegex',
            'options' => array(
                'regex' => '/catalog/(?<city>[p{L}]+)',
                // or if you prefer, your original regex should work too
                // 'regex' => '/catalog/(?<city>[a-zA-Z0-9_-äöüÄÖÜß]*)',
                'defaults' => array(
                    'controller' => 'CatalogControllerCatalog',
                    'action'     => 'list-sports',
                ),
                'spec'  => '/catalog/%city%',
            ),
            'may_terminate' => true,
        ),
    ),
),
这篇关于如何在 Zend Framework 2 中为 RegEx 路由的 RegEx 设置 (UTF8) 修饰符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Zend Framework 2 中为 RegEx 路由的 RegEx 设置 (UTF8) 修饰符?
 
				
         
 
            
        - Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
