简体   繁体   中英

zend framework 2 Routing with UTF-8 Regex Pattern

I want my routing support persian character but when use x{600}-\\x{6FF} in regex path,app give me a 404 Error (Page not found)

My zf2 routing config :

  'result' => array(
                'type'    => 'segment',
                'options' => array(                    
                    'route'    => '/Result[/:name]',
                    'constraints' => array(
                        'name' => '[\sa-zA-Z0-9_.-x{600}-\x{6FF}]*',

                    ),
                    'defaults' => array(
                        'controller' => 'Result',
                        'action'     => 'index',
                    ),
                ),
            ),

and I add u to end of regex in Zend\\Mvc\\Router\\Http\\Segment.php

if ($pathOffset !== null) {
            $result = preg_match('(\G' . $regex . ')u', $path, $matches,null, $pathOffset);
        } else {
            $result = preg_match('(^' . $regex . '$)u', $path, $matches);
        }

My URL like this :

http://localhost/test/Result/A.8.کلمه

You have a misplaced hyphen inside a character class issue and a missing \\ before the first x{600} . Here is how the hyphen changes the meaning of the pattern :

在此输入图像描述

To fix it, you need to move it to the end of the character class:

'[\sa-zA-Z0-9_.\x{600}-\x{6FF}-]*'

See another demo .

Note that if you need to add any more characters to the pattern, you should be careful with [ , ] , - , / . They must be escaped to avoid issues. Eg, adding a % :

'[%\sa-zA-Z0-9_.\x{600}-\x{6FF}-]*'

Next issue is that you need to pass rawurldecode($path) to the preg_match instead of just $path .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM