Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
208 views
in Technique[技术] by (71.8m points)

php - How to set a (UTF8) modifier for RegEx of a RegEx Route in Zend Framework 2?

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,
        ),
    ),
),

But when I set it, the route stopps to work at all (error 404) -- neither for URIs with nor to ones without special characters.

How to set the modifier correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
}

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,
        ),
    ),
),

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...