首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >独立路由库?

独立路由库?
EN

Stack Overflow用户
提问于 2012-10-28 19:15:37
回答 1查看 1.7K关注 0票数 2

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

有没有独立的php路由库?

如果没有,你能为它的开发提供什么指导方针吗?

我想要像F3框架这样的东西:

代码语言:javascript
复制
$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)

编辑

我自己开发的。下面是示例用法:

代码语言:javascript
复制
   // 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();

如果你需要一个简单的控制器,你可以直接运行一个函数,而不必指定一个类。示例:

代码语言:javascript
复制
   // 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:

代码语言:javascript
复制
   //> This will match something like this: article/123/your-title-here
   ->add('(article)/([0-9]+)/[a-z0-9-]+')

您可以从自定义控制器运行自定义方法,如下所示:

代码语言:javascript
复制
   ->add('(ctrlname)/(methodname)/(params)', array('CustomControllerName','CustomMethod') );

来源:http://pastebin.com/9F02GEyN

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-28 19:24:50

您既可以使用Symfony Routing component,也可以使用brilliant PHP路由器,后者是一个封装在单个klein.php文件中的受Sinatra启发的工具。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13108345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档