URIs with german special characters don't work (error 404) in Zend Framework 2

前端 未结 1 1975
半阙折子戏
半阙折子戏 2020-12-11 10:31

I want to get a list of cities, where each city name is linked and refers a page for this city:

\"enter

相关标签:
1条回答
  • 2020-12-11 11:25

    You need to change your constraints, you can use a regular expression which will match UTF8 characters, something like this:

    '/[\p{L}]+/u'
    

    Notice the /u modifier (unicode).

    EDIT:

    The problem is resolved.

    Explanation:

    The RegEx Route maches the URIs with preg_match(...) (line 116 or 118 of Zend\Mvc\Router\Http\Regex). In order to mach a string with "special chars" (128+) one must pass the pattern modifier u to preg_match(...). Like this:

    $thisRegex = '/catalog/(?<city>[\p{L}]*)';
    $regexStr = '(^' . $thisRegex . '$)u'; // <-- here
    $path = '/catalog/Nürnberg';
    $matches = array();
    preg_match($regexStr, $path, $matches);
    

    And since RegEx Route passes a url-enccoded string to preg_match(...), it's furthermode needed to decode the string first:

    $thisRegex = '/catalog/(?<city>[\p{L}]*)';
    $regexStr = '(^' . $thisRegex . '$)u';
    $path = rawurldecode('/catalog/N%C3%BCrnberg');
    $matches = array();
    preg_match($regexStr, $path, $matches);
    

    These two steps are not provided in the RegEx Route, so that preg_match(...) gets a steing like '/catalog/N%C3%BCrnberg' and tries to mach it to a regex like '/catalog/(?<city>[\\p{L}]*)/u'

    The solution is to use a custom RegEx Route. Here is an example.

    0 讨论(0)
提交回复
热议问题