多年来,我已经开发了自己的框架。它缺少什么是中央路由系统。 我想将一个独立的路由库集成到我的框架中,而不是重新发明轮子。
是否有某个独立的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
您当然可以使用以下搜索引擎友好网址:
//> 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') );