基本上多年来我已经开发了自己的框架。它缺少的是中央路由系统。我想将一个独立的路由库集成到我的框架中,而不是重新发明轮子。
是否有独立的 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