40

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.

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.

Is there a good library out there that only does routing? Or am I stuck with importing a full framework or reinventing the wheel?

abeger
  • 6,766
  • 7
  • 41
  • 58

7 Answers7

29

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

Kartik
  • 9,463
  • 9
  • 48
  • 52
  • 4
    Version 2 of Klein is no longer a single file. However, version 1 is still available here: https://github.com/chriso/klein.php/tree/v1.2.0 – Jeff Clemens Sep 05 '13 at 17:50
  • 2
    Another one : https://github.com/auraphp/Aura.Router/ , by the way also see the benchmark results below the performance of klein with aura http://stackoverflow.com/a/21013740/487878 – Hari K T Jul 13 '14 at 05:02
19

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.

DaGhostman Dimitrov
  • 1,608
  • 20
  • 44
7

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.

Geoffrey Hale
  • 10,597
  • 5
  • 44
  • 45
Anonymous
  • 71
  • 1
  • 1
6

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

mAsT3RpEE
  • 1,818
  • 1
  • 17
  • 14
5

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

CoR
  • 3,826
  • 5
  • 35
  • 42
3

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.

c9s
  • 1,888
  • 19
  • 15
  • 3
    Your test is extremely unrealistic with only one single simple route. You should provide a new one with ~100 real world routes and a better testing strategy. Anyone considering Pux should read this: http://nikic.github.io/2014/02/18/Fast-request-routing-using-regular-expressions.html – Andreas Bergström Apr 11 '15 at 05:01
1

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

powtac
  • 40,542
  • 28
  • 115
  • 170