我有路由数组:
$route = array(
'/home/news/([0-9])'=>'/home/news/id/$1'
);
我有检查请求和验证的脚本。所以我可以运行:
http://mysite.com/home/news/id/4 (run: controller: home, action: news, param id: 4 -> this is defaults)
和
http://mysite.com/home/news/4 (run: controller: home, action: news, param id: 4 -> look route array [up])
脚本:
$request = '/home/news/id/10'; //example
foreach($route as $r => $key) {
$r = str_replace('/', '\\/', $r);
if(preg_match('/^'.$r.'$/',$request)) {
$request = preg_replace('/^'.$r.'$/i',$key,$request);
break;
}
所以我现在想要全部反转:
function createUrl($array) {
//search in route value and if is ok return key array
}
例子:
echo createUrl(array('controller'=>'home','action'=>'news','id'=>4)); //if not exists in route return: /home/news/id/4 but if exists reutnr /home/news/4
对不起我的英语:D