

Link: A fast and simple PHP Router - apsdehal
https://github.com/apsdehal/Link
Link supports RESTful routes and helps you create clean Web apps and APIs through its various features like Named Routes.
Its an effort to combine good parts of various php router, like toro&#x27;s method of parsing routes, laravel&#x27;s named routes, others middleware etc. so much.
======
nubs
The claim of "fast" definitely looks inaccurate in comparison to nikic's
router[0]. I'd recommend reading through his blog[1] on the matter.

[0]: [https://github.com/nikic/FastRoute](https://github.com/nikic/FastRoute)

[1]: [http://nikic.github.io/2014/02/18/Fast-request-routing-
using...](http://nikic.github.io/2014/02/18/Fast-request-routing-using-
regular-expressions.html)

~~~
apsdehal
Thanks for your concern, this router is a result out of a effort to learn
about routing. I will try to add benchmarks soon.

------
leeoniya
there are so many php routers these days that any "fast" claims should
probably be accompanied by benchmarks.

routing is a fairly uncomplicated task and the speed difference will only
become apparent at really high loads.

[http://c9s.github.io/Pux/](http://c9s.github.io/Pux/)

[http://auraphp.com/packages/Aura.Router/](http://auraphp.com/packages/Aura.Router/)

[http://zaphpa.org/](http://zaphpa.org/)

[http://toroweb.org/](http://toroweb.org/)

[https://github.com/dannyvankooten/PHP-
Router](https://github.com/dannyvankooten/PHP-Router)

[https://github.com/symfony/Routing](https://github.com/symfony/Routing)

[http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Router....](http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Router.html)

~~~
nubs
This list is missing
[https://github.com/nikic/FastRoute](https://github.com/nikic/FastRoute) which
is definitely worth looking at.

~~~
Nickoladze
Dont forget µ which is totally a legit router

[https://github.com/lastguest/mu](https://github.com/lastguest/mu)

~~~
insky
Which begs the question, how on earth do you choose one?

Edit: as in, which router do you pick and how do you pick one? Choice can lead
to decision paralysis.

~~~
apsdehal
Check whats your need, if you are building up much bigger apps go for a fully
fledged framework, rest for small apps you can use microrouters , according to
your need, each has its own unique features.

~~~
icedchai
For small apps, you don't need a "router", period. Routing is done by the file
system and placing .php files in directories!

~~~
krapp
One of the benefits of using a url router is access control - you could have a
problem with just letting every file be executable by url, especially if you
happen to have a remote file inclusion issue anywhere. With a router you know
explicitly what can get invoked and what can't.

Although, doing it without a router is probably always going to be faster.

~~~
insky
A late trick I discovered, was that you can configure your php instance to
inclued a file with every script. For example the following can be added to
.htaccess:

    
    
      php_value auto_prepend_file 'before.php'
    

You can then use that for bootstrapping, front controller and to limit access.
Can be a bit clumsy, but it's nicely divorced from simple page controllers.

~~~
icedchai
neat! this beats putting an include at the top of each file...

~~~
krapp
It certainly does.

I personally would still prefer a router but I know of a project which was
written almost entirely in sprawling, procedural PHP that might benefit from
this. Although I do wonder what the performance hit would be.

------
mcfunley
FWIW at Etsy I built and subsequently helped completely dismantle a minimal
PHP routing framework. We just used .htaccess and php files.

PHP/Apache affords you a fast, simple alternative, and what you gain with a
framework (route lookup for an object) just isn't compelling enough IMO to
justify the added overhead and complexity.

I think the endless proliferation of PHP microrouters (including mine, RIP) is
people porting Django and Rails features to PHP without applying critical
thinking first. (Myself included.)

~~~
127001brewer
How _complicated_ were the Etsy routes? And were the added overhead and
complexity?

And I know it is a pain to restart Apache each time there's a change to the
configuration file, but does anyone know the time differences between a PHP
router and Apache rewrites?

~~~
icedchai
Put it in .htaccess. No restarts needed.

~~~
127001brewer
That's correct - I was thinking of using an Apache VirtualHost file. (Isn't
using the .htaccess file slower(?) than using the VirtualHost file?)

~~~
icedchai
It is slower, since .htaccess is parsed on every request. (However, it is
probably faster than routing through PHP code.)

------
127001brewer
Why would you want to use _any_ PHP router instead of writing the rewrite
rules within an Apache VirtualHost file or a .htaccess file?

~~~
vectorpush
Separation of concerns? Why would I ever _want_ to implement business logic in
an http server configuration file?

~~~
mjburgess
Umm.. what do you think a _server_ is for?

~~~
vectorpush
The http server's job is to translate URLs into http requests for consumption
by the application. The http server should generally be agnostic regarding the
semantic meaning of your URLs, which take the form of business rules, the
proper place for which is the application layer (whenever possible).

------
erming
Looks like everyone and their grandma has written their own PHP router.

Here's mine:

[https://github.com/erming/route](https://github.com/erming/route)

Code:
[https://github.com/erming/route/blob/master/route.php](https://github.com/erming/route/blob/master/route.php)

~80 LOC

~~~
krapp
I did. It's not worth posting though.

I think writing a php router is one of those things everyone who works with
php should learn to do. It teaches you a lot.

------
insky
How does this compare with something like Slim?

+1 for being lightweight and readable.

~~~
darkstar999
Slim is a whole (micro)frameork that includes routing. This is just a request
router.

------
jamesmoss
Meh, another PHP library using lengthy static methods making it impossible to
extend.

~~~
andrewryno
Yeah I don't understand the use of statics here. I'm going to guess it's just
so they can use the "nicer" syntax without having to instantiate a new object,
which would honestly make more sense to me. That way you can inject a Link
instance into your controller/views and just call
`$link->route('namedRoute')`.

~~~
insky
It looks tidier with the global.

------
alphadevx
Nice work! A micro-micro-framework, makes Slim and Silex look big.

~~~
arenaninja
It is nice, but having implemented something like this myself at work
recently, this looks eerily familiar and some things about it bug me.

For example, why is ReflectionClass not used (I'm guessing PHP 4
compatibility)? Particularly because class_exists can cause major headaches if
you use the default autoloader, see
[https://bugs.php.net/bug.php?id=52339](https://bugs.php.net/bug.php?id=52339)

There's also little information available from outside the router; an invalid
class/method uses die without giving you an option to check for it and handle
it some other way. I also don't immediately see any special handling for not
allowing magical methods to be called

Definitely a good start for personal projects though, just needs some spit and
polish

~~~
apsdehal
Thanks for your concern, I would try to implement features you have said, Its
still under development mode and needs much polish.

~~~
arenaninja
Good luck! Another feature of my router is that the method name converts
snake_case to camelCase, so that resource/list_actions calls
ResourceController::listActions(), but some of the features I mention will
most likely incur a performance hit, but it shouldn't be significant once
opcache comes into play

------
pothibo
Is it a PHP thing to indent with tabs rather than 2 spaces? Genuinely curious.

~~~
mjburgess
Nope, it's a ruby thing I believe.

~~~
leeoniya
or maybe it's a javascript thing
[http://code.jquery.com/jquery-2.1.1.js](http://code.jquery.com/jquery-2.1.1.js)

or maybe it's just a trolling thing.

------
darkstar999
No tests?

~~~
pearjuice
It's written in PHP.

~~~
nawariata
Your nick should read prejudice.

