Is there an standalone PHP routing library? [closed]

别说谁变了你拦得住时间么 提交于 2019-11-28 03:15:50

Try Klein:

Single file, standalone and robust:

"klein.php is a lightning fast router for 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

https://github.com/chriso/klein.php

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.

The 2 solutions which came to my mind as soon as I saw your question ware:

Phroute is built on top of FastRoute, hence they both require 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)

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.

Anonymous

Try 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.

I don't require a full-blown framework at all. If you need an ORM, try Redbean.

If you need a template engine use Twig.

With this approach you just install what you use.

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.

https://github.com/nikic/FastRoute

PHP – seriously simple Router is really nice and simple.
PHP – RegexRouter is even better in my opinion :)

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:

$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).
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.

With Pux PHP Extension support, you may load and dispatch the routes 1.5~2x faster than pure PHP Pux.

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()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!