Regex CakePHP为unicode路由正则表达式
我想在CakePHP路由中做以下更改。像Regex CakePHP为unicode路由正则表达式,regex,cakephp,unicode,routes,Regex,Cakephp,Unicode,Routes,我想在CakePHP路由中做以下更改。像example.com/someword或example.com/someword这样的网站URL之后的任何单词或词组(用连字符分隔)都应该重定向到controller=>posts,action=>view。但我的问题是,单词应该是unicode。我已经尝试了URL文本的正则表达式,但它不起作用 路由器::connect('/:link',array('controller'=>'posts','action'=>'view'),array('pass'
example.com/someword
或example.com/someword
这样的网站URL之后的任何单词或词组(用连字符分隔)都应该重定向到controller=>posts,action=>view
。但我的问题是,单词应该是unicode。我已经尝试了URL文本的正则表达式,但它不起作用
路由器::connect('/:link',array('controller'=>'posts','action'=>'view'),array('pass'=>array('link'),'link'=>'/^['.json_decode('''\u0531').-'.json_decode('\u0587').-]+$/u')
不过我想提一下,我的正则表达式
'/^['.json_decode('"\u0531"').'-'.json_decode('"\u0587"').'-]+$/u',
如果我要做一些常规的赛前比赛,比如
$regg = '/^['.json_decode('"\u0531"').'-'.json_decode('"\u0587"').'-]+$/u';
if (preg_match($regg, "my unicode string", $match)) {
var_dump($match); // outputs string as expected
}
die;
编辑
我最终得到了这个代码
Router::connect ( '/:link', array ('controller' => 'posts', 'action' => 'view' ), array ('pass' => array ('link'), 'link' => '((?![a-zA-Z]).)+' ) );
就像让我的url包含所有内容,但不包括英文字母,但这不是我需要的东西,如果我有两种或更多不同的语言,而不是英语-这种方法将不起作用。所以,我想把每一种语言的话语都分别付诸行动
蛋糕2.x版
编辑2
有其他的解决方案,如下面的答案,可以绕过这个问题,但我正在寻找做我需要的事情的确切方法
感谢您在at URL编码段落中,您不能在URL查询字符串上使用以下字符:
具体而言,对查询字符串进行编码时使用以下规则:
- 字母(A–Z和A–Z)、数字(0–9)和字符“.”、“-”、“~”和“u”保持原样
- 空格编码为“+”或“%20”[8]
- 所有其他字符编码为%HH十六进制表示,任何非ASCII字符首先编码为UTF-8(或其他指定字符) 编码)
如果你有两种(或更多种)语言,你可以考虑附加的POST表,每个语言都有一个SLUG字段,例如英语和俄语:
职位- 身份证
- 标题(英文)
- 标题(rus)
- slug_eng
- 鼻涕虫
if(isset($this->data['Post']['title_eng'])){
$this->data['Post']['slug_eng'] = Inflector::slug(strtolower($this->data['Post']['title_eng']), '-');
}
if(isset($this->data['Post']['title_rus'])){
$this->data['Post']['slug_rus'] = Inflector::slug(strtolower($this->data['Post']['title_rus']), '-');
}
视图中的链接:
echo $this->Html->link($post['Post']['title_eng'], array('controller' => 'posts', 'action' => 'view', 'slug_eng' => $post['Post']['slug_eng']));
echo $this->Html->link($post['Post']['title_rus'], array('controller' => 'posts', 'action' => 'view', 'slug_rus' => $post['Post']['slug_rus']));
路线:
Router::connect('/posts/:slug_eng', array('controller' => 'posts','action' => 'view', 'eng'), array('pass' => array('slug_eng'), 'slug_eng' => '[a-zA-Z0-9]+'));
Router::connect('/ru/posts/:slug_rus', array('controller' => 'posts','action' => 'view', 'rus'), array('pass' => array('slug_rus'), 'slug_rus' => '[a-zA-Z0-9]+'));
主计长派驻:
public function view($locale, $slug){
....
validate and sanitize here
...
$post = $this->Post->find('first', array('conditions' => array('slug_' . $locale => $slug)));
....
}
我建议使用通配符
Router::connect('/*', array('controller' => 'posts', 'action' => 'view'));
例如,可以在此行之前添加任何其他路线
Router::connect('/:digits', array('controller' => 'posts', 'action' => 'view'), array('pass' => array('digits'), 'digits' => '/d+');
Router::connect('/*', array('controller' => 'posts', 'action' => 'view'));
感谢您的回答,请参阅,您可以将其他字符编码为十六进制,但是在任何情况下,如何编写正则表达式(尤其是在cake中)以使其在cake中工作,这就是问题所在。替代方案看起来不错,但它不是我需要的。谢谢你的回答,这又是一个选择,但不是我需要的。谢谢