基本上,多年来,我已经开发了自己的框架。它缺少的是一个中央路由系统。我想将一个独立的路由库集成到我的框架中,而不是重新发明轮子。
有没有独立的php路由库?
如果没有,你能为它的开发提供什么指导方针吗?
我想要像F3框架这样的东西:
$route->add( 'article/view/[0-9]+' ); //> Call Article->view(); (website.net/article/id/123)
$route->add( 'email', 'email.php' ); //> Run email.php (website.net/email)编辑
我自己开发的。下面是示例用法:
// index.php
require 'router.php';
$router = new Router();
$router
//> It will require controllers/article.php and call one of the view,etc method
->add('(article)/(view|edit|delete|add)/([0-9]+)')
//> Same thing as before, but this time we use underscore as separator
//> It will require controllers/entry.php and call view method
->add('(entry)_(view)_([0-9]+)')
//> Or you can require custom file like this
->add( '(myCustomPage)' , '/controllers/myCustomPath/myPage.php' )
->dispatch();如果你需要一个简单的控制器,你可以直接运行一个函数,而不必指定一个类。示例:
// myCustomController.php
function myCustomController($id) {
echo 'I am the Custom Controller';
}
// index.php
$router
->add('(myCustomController)/([0-9]+)');
// The routing system will detect there is a function and will call it directly.
// Otherwise will just instanciate a new myCustomController() object当然,您可以像这样使用搜索引擎友好的URL:
//> This will match something like this: article/123/your-title-here
->add('(article)/([0-9]+)/[a-z0-9-]+')您可以从自定义控制器运行自定义方法,如下所示:
->add('(ctrlname)/(methodname)/(params)', array('CustomControllerName','CustomMethod') );来源:http://pastebin.com/9F02GEyN
发布于 2012-10-28 19:24:50
您既可以使用Symfony Routing component,也可以使用brilliant PHP路由器,后者是一个封装在单个klein.php文件中的受Sinatra启发的工具。
https://stackoverflow.com/questions/13108345
复制相似问题