Php 反向预加更换-路由器
我有一个带有路由的阵列:Php 反向预加更换-路由器,php,preg-replace,router,Php,Preg Replace,Router,我有一个带有路由的阵列: $route = array( '/home/news/([0-9])'=>'/home/news/id/$1' ); 我还有支票申请的脚本&valide。所以我可以跑: http://mysite.com/home/news/id/4 (run: controller: home, action: news, param id: 4 -> this is defaults) 及 脚本: $request = '/home/new
$route = array(
'/home/news/([0-9])'=>'/home/news/id/$1'
);
我还有支票申请的脚本&valide。所以我可以跑:
http://mysite.com/home/news/id/4 (run: controller: home, action: news, param id: 4 -> this is defaults)
及
脚本:
$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你为什么不简单地创建:
function createUrl($route) {
return $route['controller'].'/'.$route['action'].'/'.$route['id'] ;
}
然后,如果显示信息:
createurl(array('controller'=>'home','action'=>'news','id'=>2));
你会得到你期望的结果。
您还可以在函数中添加一个验证器来检查输入参数。因此,基本上我们必须猜测这里的问题以及
createurl()中的任何内容。
?请在您的请求中更加清楚,我不理解这个问题。
createurl(array('controller'=>'home','action'=>'news','id'=>2));