是否有独立的 PHP 路由库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15392024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an standalone PHP routing library?
提问by abeger
I'm looking to add some dynamic, REST-esque routing to a PHP application. I'd love to use an existing routing library so I don't have to reinvent the wheel.
我希望向 PHP 应用程序添加一些动态的、REST 风格的路由。我很想使用现有的路由库,这样我就不必重新发明轮子了。
However, when I look at things like Slim and F3, they all come with things I don't want--like templating and MVC--included. Since I just want routing, I'd end up with a lot of framework code in my application that I don't need.
然而,当我看到像 Slim 和 F3 这样的东西时,它们都带有我不想要的东西——比如模板和 MVC——包括在内。因为我只想要路由,所以我的应用程序中最终会出现很多我不需要的框架代码。
Is there a good library out there that only does routing? Or am I stuck with importing a full framework or reinventing the wheel?
有没有一个好的库只做路由?还是我坚持导入完整的框架或重新发明轮子?
采纳答案by Kartik
Try Klein:
试试克莱因:
Single file, standalone and robust:
单个文件,独立且健壮:
"klein.php is a lightning fast router for PHP 5.3+"
“klein.php 是适用于 PHP 5.3+ 的闪电般的路由器”
- Flexible regular expression routing (inspired by Sinatra)
- A set of boilerplate methods for rapidly building web apps
- Almost no overhead => 2500+ requests/second
- 灵活的正则表达式路由(受 Sinatra 启发)
- 一组用于快速构建 Web 应用程序的样板方法
- 几乎没有开销 => 2500+ 个请求/秒
回答by DaGhostman Dimitrov
Due to the oldish answers on this question I think it would be a pretty good idea to mention some more up-to-date solutions to the case in the OP.
由于这个问题的旧答案,我认为在 OP 中提及一些更新的案例解决方案是一个不错的主意。
The 2 solutions which came to my mind as soon as I saw your question ware:
我一看到你的问题就想到了两个解决方案:
- FastRoutehttps://github.com/nikic/FastRoute- Written by Nikita Popov
- Phroutehttps://github.com/mrjgreen/phroute- Written by Joe Green
- FastRoute https://github.com/nikic/FastRoute- 由 Nikita Popov 撰写
- Phroute https://github.com/mrjgreen/phroute- 由 Joe Green 撰写
Phroute is built on top of FastRoute, hence they both require PHP 5.4.
Phroute 建立在 FastRoute 之上,因此它们都需要 PHP 5.4。
If you need a PHP 5.3+ solution, I would definitely recommend Slim Framework's routing. If you don't want any of the other functionality which come with the framework, you might extract the Routing parts and use only them (SLIM is MIT licensed, so you are allowed to do whatever)
如果您需要 PHP 5.3+ 解决方案,我肯定会推荐 Slim Framework 的路由。如果您不想要框架附带的任何其他功能,您可以提取路由部分并仅使用它们(SLIM 是 MIT 许可的,因此您可以做任何事情)
Ive used the slim routing standalone, in a project of mine - DaGhostman\CodeWave @ github, see tags <=2.4, the relative parts are in Application\Core& Application\Controller.
我在我的一个项目 - DaGhostman\CodeWave @ github 中单独使用了纤薄路由,参见标签 <=2.4,相关部分在Application\Core& 中Application\Controller。
回答by Anonymous
Try Dispatch: https://github.com/noodlehaus/dispatch
尝试调度:https: //github.com/noodlehaus/dispatch
require 'dispatch.php';
get('/', function () {
echo "Hello world!\n";
});
dispatch();
It's a single-file routing framework for PHP. Very light-weight, very easy to work with. This one and Slim are my favorites.
它是一个用于 PHP 的单文件路由框架。非常轻巧,非常容易使用。这个和 Slim 是我的最爱。
I don't require a full-blown framework at all. If you need an ORM, try Redbean.
我根本不需要成熟的框架。如果您需要 ORM,请尝试 Redbean。
If you need a template engine use Twig.
如果您需要模板引擎,请使用 Twig。
With this approach you just install what you use.
使用这种方法,您只需安装您使用的内容。
回答by CoR
PHP – seriously simple Routeris really nice and simple.
PHP – RegexRouteris even better in my opinion :)
PHP – 非常简单 路由器真的很好很简单。
PHP – RegexRouter在我看来甚至更好:)
回答by mAsT3RpEE
Fastroute is a standalone routing library. It's based on an OOP model and has a full testing framework. Open Source and Licence is free. If you are looking for something to modify for your own projects this is a good place to start.
Fastroute 是一个独立的路由库。它基于 OOP 模型并具有完整的测试框架。开源和许可证是免费的。如果您正在为自己的项目寻找一些东西来修改,这是一个很好的起点。
回答by c9s
Check out Pux ( https://github.com/c9s/Pux), which is targeted at extreme high performance , zero dependency, zero-overhead (with C extension support). while providing good performance, Pux also provides a Sinatra-like API for you to define your own routing paths:
查看 Pux ( https://github.com/c9s/Pux),它的目标是极高性能、零依赖、零开销(支持 C 扩展)。在提供良好性能的同时,Pux 还提供了类似 Sinatra 的 API 供您定义自己的路由路径:
$mux = new Pux\Mux;
$mux->any('/product', ['ProductController','listAction']);
$mux->get('/product/:id', ['ProductController','itemAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ]
]);
$mux->post('/product/:id', ['ProductController','updateAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ]
]);
$mux->delete('/product/:id', ['ProductController','deleteAction'] , [
'require' => [ 'id' => '\d+', ],
'default' => [ 'id' => '1', ]
]);
$route = $mux->dispatch('/product/1');
The benchmark result:
基准测试结果:
- 48.5x faster than "symfony/routing" in static route dispatching. (with pux extension installed)
- 31x faster than "symfony/routing" in regular expression dispatching. (with pux extension installed)
- 69x faster than "klein" (with pux extension installed).
- 在静态路由调度中比“symfony/routing”快 48.5 倍。(安装了 pux 扩展)
- 在正则表达式调度中比“symfony/routing”快 31 倍。(安装了 pux 扩展)
- 比“klein”(安装了 pux 扩展)快 69 倍。
n=10000
Runing php array - . 138796.45654569/s
Runing pux - . 124982.98519026/s
Runing klein - . 1801.5070399717/s
Runing ham - . 13566.734991391/s
Runing aura - . 39657.986477172/s
Runing symfony/routing - . 1934.2415677861/s
Rate Mem php array pux aura ham symfony/routing klein
php array 138.8K/s 0B ---90% -28% -9% -1% -1%
pux 124.98K/s 0B 111% -- -31%-10% -1% -1%
aura 39.66K/s 0B 349%315% ---34% -4% -4%
ham 13.57K/s 0B 1023%921% 292% -- -14% -13%
symfony/routing 1.93K/s 786K 7175%6461%2050%701% -- -93%
klein 1.8K/s 262K 7704%6937%2201%753% 107% --
================================== Bar Chart ==================================
php array 138.8K/s | ████████████████████████████████████████████████████████████ |
pux 124.98K/s | ██████████████████████████████████████████████████████ |
aura 39.66K/s | █████████████████ |
ham 13.57K/s | █████ |
symfony/routing 1.93K/s | |
klein 1.8K/s | |
============================== System Information ==============================
PHP Version: 5.5.6
CPU Brand String: Intel(R) Core(TM) i5-3427U CPU @ 1.80GHz
With XDebug Extension.
Pux tries not to consume computation time to build all routes dynamically (like Symfony/Routing). Instead, Pux compiles your routes to plain PHP array for caching, the compiled routes can be loaded from cache very fast.
Pux 尽量不消耗计算时间来动态构建所有路由(如 Symfony/Routing)。相反,Pux 将您的路由编译为纯 PHP 数组进行缓存,编译后的路由可以非常快地从缓存中加载。
With Pux PHP Extension support, you may load and dispatch the routes 1.5~2x faster than pure PHP Pux.
借助 Pux PHP 扩展支持,您可以比纯 PHP Pux 快 1.5~2 倍的速度加载和调度路由。
回答by powtac
Have a look at the Router class of the lithium framework, since makes heavy use of dependency injections you can configure the Router class, by calling config()
看看锂框架的Router 类,因为大量使用依赖注入,你可以配置 Router 类,通过调用config()

