我正在寻找一种方法来自动避免 cakephp url 中的 slug 和控制器/插件之间的干扰。根据这篇文章(http://lecterror.com/articles/view/advanced-routing-with-cakephp-one-example),我的 route.php 文件如下所示:
$exceptions = Cache::read('exception_url_list');
if ($exceptions===false) {
$controllers=App::objects('Controller') ;
$plugins=App::objects('plugin');
$i=0 ;
foreach ($controllers as $controller) {
$list[$i]=str_replace('Controller','',$controller) ;
$i++ ;
}
$exceptions=array_merge($list,$plugins) ;
$i=0 ;
foreach ($exceptions as $value) {
$value = Inflector::underscore($value);
$value = strtolower($value) ;
$list[$i]=$value ;
$i++ ;
}
$exceptions=implode('|', $list) ;
Cache::write('exception_url_list',$exceptions) ;
}
Router::connect('/:language/:typeslug',
array('controller' => 'nodetypes', 'action' => 'view'),
array(
'language'=>'[a-z]{3}',
'typeslug' => '(?!('.$exceptions.')((\W+)|$))[a-zA-Z\-_]+/?$',
'pass'=>array('typeslug')
)
);
Router::connect('/:language/:typeslug/:nodeslug',
array('controller' => 'nodes', 'action' => 'view'),
array(
'language'=>'[a-z]{3}',
'typeslug' => '(?!('.$exceptions.')((\W+)|$))[a-zA-Z\-_]+/?$',
'pass'=>array('typeslug','nodeslug')
)
);
第一条路线效果很好,但第二条路线不是蛋糕会搜索与语言前缀相同的控制器名称!