如何设置一个正则表达式路由的正则表达式的一个(UTF8)修改Zend框架2?(How to set

2019-07-28 09:05发布

我在用的URI(德国),特殊字符的烦恼 ,想尝试用解决它正则表达式路线和PCRE模式修改为UTF-8 u

'router' => array(
    'routes' => array(
        // ...
        'city' => array(
            'type'  => 'regex',
            'options' => array(
                'regex' => '/catalog/(?<city>[a-zA-Z0-9_-äöüÄÖÜß]*)\/u',
                'defaults' => array(
                    'controller' => 'Catalog\Controller\Catalog',
                    'action'     => 'list-sports',
                ),
                'spec'  => '/catalog/%city%',
            ),
            'may_terminate' => true,
        ),
    ),
),

但是,当我将它设置,路线stopps在所有的(错误404)的工作 - 既不用也没有的人没有特殊字符的URI。

如何正确设置修改?

Answer 1:

既然我已经有了这个开放这里是解决该问题的处理程序。

<?php
namespace Application\Mvc\Router\Http;

use Zend\Mvc\Router\Http\Regex;
use Zend\Mvc\Router\Http\RouteMatch;
use Zend\Stdlib\RequestInterface 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你的路由定义应该是这样的

'router' => array(
    'routes' => array(
        // ...
        'city' => array(
            'type'  => 'Application\Mvc\Router\Http\UnicodeRegex',
            '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' => 'Catalog\Controller\Catalog',
                    'action'     => 'list-sports',
                ),
                'spec'  => '/catalog/%city%',
            ),
            'may_terminate' => true,
        ),
    ),
),


Answer 2:

好,

我想你可以一样容易解决这个问题许多其他的有同样的问题。 所以,看看其中的一些:

UTF-8 *正则表达式

有使用类似下面的修饰符\\s\\p{L}\\u帮助你。 我希望它解决了! 祝好运。

编辑

看到我自己的测试:

<?php

    $toss_the_dice = utf8_decode ("etc/catalog/Nürnberg");
    preg_match ('/\/catalog\/([\\s\\p{L}]*)/m', $toss_the_dice, $dice);
    echo utf8_encode ($dice[1]);

// Now it prints
// Nürnberg

?>

可以实现?

编辑2

它可以对你更好!

<?php
    $toss_the_dice = "etc/catalog/Nürnberg";
    preg_match ('/\/catalog\/([\\s\\p{L}]*)/u', $toss_the_dice, $dice);
    echo $dice[1];

// Now it also prints
// Nürnberg

?>


文章来源: How to set a (UTF8) modifier for RegEx of a RegEx Route in Zend Framework 2?