是否有可重用的PHP路由器/调度程序?
我使用一个简单的框架,它根据查询参数处理请求是否有可重用的PHP路由器/调度程序?,php,url-routing,Php,Url Routing,我使用一个简单的框架,它根据查询参数处理请求 http://example.com/index.php?event=listPage http://example.com/index.php?event=itemView&id=1234 我想将干净的URL放在前面,这样您就可以通过以下方式访问它: http://example.com/list http://example.com/items/1234 我知道路线和调度是如何工作的,我可以自己写。但是我宁愿利用所有已经解决了这个问题的代码。
http://example.com/index.php?event=listPage
http://example.com/index.php?event=itemView&id=1234
我想将干净的URL放在前面,这样您就可以通过以下方式访问它:
http://example.com/list
http://example.com/items/1234
我知道路线和调度是如何工作的,我可以自己写。但是我宁愿利用所有已经解决了这个问题的代码。是否有人知道提供此功能的泛型库或类,但允许我从路由匹配返回我想要的任何内容?像这样的
$Router = new Router();
$Router->addRoute('/items/:id', 'itemView', array( 'eventName' => 'itemView' ));
$Router->resolve( '/items/1234' );
// returns array( 'routeName' => 'itemView',
// 'eventName' => 'itemView,
// 'params' => array( 'id' => '1234' ) )
基本上,我可以根据从路径解析的值自己进行调度。如果不太麻烦的话(只要许可证允许),我不介意将其从框架中移除。但通常我发现框架中的路由/调度过于集成,无法像这样重新调整用途。我的搜索结果似乎表明,如果人们不使用框架,他们自己也在写这篇文章
一个好的解决方案将支持以下内容:
- 使用冒号符号或正则表达式符号指定路由
- 解析路由之外的参数并以某种方式返回它们
- 支持快速反向查找,如下所示:
$Router->get( 'itemView', array( 'id' => '1234' ) ); // returns 'items/1234'
require_once('glue.php');
$urls = array(
'/' => 'index',
'/(?P<number>\d+)' => 'index'
);
class index {
function GET($matches) {
if (array_key_exists('number', $matches)) {
echo "The magic number is: " . $matches['number'];
} else {
echo "You did not enter a number.";
}
}
}
glue::stick($urls);
require_once('glue.php');
$URL=数组(
“/”=>“索引”,
“/(?P\d+”=>“索引”
);
类索引{
函数GET($matches){
如果(数组\键\存在('number',$matches)){
echo“神奇的数字是:”.$matches['number'];
}否则{
echo“您没有输入数字。”;
}
}
}
胶水:粘贴($URL);
您还可以查看Symfony2布线组件!看一看,答案和。谢谢。这很简单,我可能可以根据需要调整它。我希望路由和调度是解耦的。