我正在使用一个简单的框架,它根据查询参数处理请求。
http://example.com/index.php?event=listPage
http://example.com/index.php?event=itemView&id=1234
我想将干净的网址放在前面,以便您可以通过以下方式访问它:
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' ) )
本质上,我可以根据从路径解析的值自己进行调度。如果不是太麻烦(并且只要许可证允许),我不介意将其从框架中删除。但通常我发现框架中的路由/调度有点过于集成,无法像这样重新调整用途。我的搜索似乎表明,如果人们不使用框架,他们就会自己编写此内容。
一个好的解决方案将支持以下内容:
任何帮助表示赞赏。
GluePHP http://gluephp.com/可能非常接近你想要的。它提供了一项简单的服务:将 URL 映射到类。
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)