News for hackers. Stuff that matters.

Link: A fast and simple PHP Router

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's method of parsing routes, laravel's named routes, others middleware etc. so much.
( 56 comments | 47 points | on HN )
Re:Link: A fast and simple PHP Router by nubs on Thursday May 29, 2014 @12:03PM (#7816891)
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

[1]: http://nikic.github.io/2014/02/18/Fast-request-routing-using...

Re:Link: A fast and simple PHP Router by apsdehal on Thursday May 29, 2014 @12:47PM (#7817145)
Thanks for your concern, this router is a result out of a effort to learn about routing. I will try to add benchmarks soon.
Re:Link: A fast and simple PHP Router by leeoniya on Thursday May 29, 2014 @11:57AM (#7816854)
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://auraphp.com/packages/Aura.Router/

http://zaphpa.org/

http://toroweb.org/

https://github.com/dannyvankooten/PHP-Router

https://github.com/symfony/Routing

http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Router....

Re:Link: A fast and simple PHP Router by nubs on Thursday May 29, 2014 @12:04PM (#7816894)
This list is missing https://github.com/nikic/FastRoute which is definitely worth looking at.
Re:Link: A fast and simple PHP Router by ams6110 on Thursday May 29, 2014 @01:22PM (#7817362)
I used Epiphany last time I needed something like this. https://github.com/jmathai/epiphany

Note that is more than a router, it's a microframework.

Re:Link: A fast and simple PHP Router by krapp on Friday May 30, 2014 @04:33PM (#7823902)
What would you want a 'minimally viable' microframework to include, in terms of functionality?

This has come up a couple of times in PHP framework and router threads, and i've been wondering what it would look like (it would probably be even smaller than Slim Framework). I've gotten as far as including Composer and a rather barebones router and telling people to just include whatever else they want but I suspect other people would want more.

I find it annoying when frameworks include dependencies I don't want, particularly an ORM or a templating engine, but I understand for a lot of people, that's the point (not having to pick dependencies themselves.)

Re:Link: A fast and simple PHP Router by Nickoladze on Thursday May 29, 2014 @12:45PM (#7817139)
Dont forget µ which is totally a legit router

https://github.com/lastguest/mu

Re:Link: A fast and simple PHP Router by insky on Thursday May 29, 2014 @12:50PM (#7817158)
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.

Re:Link: A fast and simple PHP Router by apsdehal on Thursday May 29, 2014 @01:23PM (#7817369)
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.
Re:Link: A fast and simple PHP Router by icedchai on Thursday May 29, 2014 @02:10PM (#7817677)
For small apps, you don't need a "router", period. Routing is done by the file system and placing .php files in directories!
Re:Link: A fast and simple PHP Router by krapp on Thursday May 29, 2014 @05:15PM (#7818624)
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.

Re:Link: A fast and simple PHP Router by insky on Friday May 30, 2014 @04:42AM (#7820831)
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.
Re:Link: A fast and simple PHP Router by icedchai on Friday May 30, 2014 @12:14PM (#7822560)
neat! this beats putting an include at the top of each file...
Re:Link: A fast and simple PHP Router by krapp on Friday May 30, 2014 @03:49PM (#7823619)
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.

Re:Link: A fast and simple PHP Router by leeoniya on Thursday May 29, 2014 @12:59PM (#7817213)
the fact that it uses the magic __callStatic probably means it's out of the running for "fastest"
Re:Link: A fast and simple PHP Router by kawsper on Thursday May 29, 2014 @01:46PM (#7817534)
I liked Klein.php as well. https://github.com/chriso/klein.php
Re:Link: A fast and simple PHP Router by chime on Thursday May 29, 2014 @03:20PM (#7818058)
Re:Link: A fast and simple PHP Router by mcfunley on Thursday May 29, 2014 @01:01PM (#7817228)
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.)

Re:Link: A fast and simple PHP Router by 127001brewer on Thursday May 29, 2014 @01:30PM (#7817419)
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?

Re:Link: A fast and simple PHP Router by mcfunley on Thursday May 29, 2014 @02:23PM (#7817744)
Basically just "/listing/<a number>/<some ignored text> goes to listing.php", multiplied by a thousand pages or so.

There's overhead associated with doing the bookkeeping necessary to compute the route lookups. In practice, it's not that awful to just write a function like "get_url($listing_id)" by hand, and debugging this later is much more straightforward.

We didn't need to restart httpd (in development at least) to add to .htaccess. For production deploys maybe we were configured to do this, but that's hidden from development.

Re:Link: A fast and simple PHP Router by icedchai on Thursday May 29, 2014 @01:31PM (#7817436)
Put it in .htaccess. No restarts needed.
Re:Link: A fast and simple PHP Router by 127001brewer on Thursday May 29, 2014 @01:38PM (#7817476)
That's correct - I was thinking of using an Apache VirtualHost file. (Isn't using the .htaccess file slower(?) than using the VirtualHost file?)
Re:Link: A fast and simple PHP Router by icedchai on Thursday May 29, 2014 @01:45PM (#7817525)
It is slower, since .htaccess is parsed on every request. (However, it is probably faster than routing through PHP code.)
Re:Link: A fast and simple PHP Router by rdam on Thursday May 29, 2014 @02:41PM (#7817829)
You can reload Apache without a restart (apache2/httpd graceful/reload depending on your distro).
Re:Link: A fast and simple PHP Router by yowmamasita on Friday May 30, 2014 @07:16AM (#7821147)
then I think what would be best here is a command line tool to manage your routes on .htaccess or nginx configs.
Re:Link: A fast and simple PHP Router by 127001brewer on Thursday May 29, 2014 @12:01PM (#7816879)
Why would you want to use any PHP router instead of writing the rewrite rules within an Apache VirtualHost file or a .htaccess file?
Re:Link: A fast and simple PHP Router by vectorpush on Thursday May 29, 2014 @12:42PM (#7817116)
Separation of concerns? Why would I ever want to implement business logic in an http server configuration file?
Re:Link: A fast and simple PHP Router by mjburgess on Thursday May 29, 2014 @01:47PM (#7817543)
Umm.. what do you think a server is for?
Re:Link: A fast and simple PHP Router by vectorpush on Thursday May 29, 2014 @03:03PM (#7817964)
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).
Re:Link: A fast and simple PHP Router by RobAley on Thursday May 29, 2014 @12:05PM (#7816895)
At a guess because you don't use apache, you don't have .htaccess / conf permissions, you are going to distribute your script and can't rely on the above, your router uses database or other external sources, you want to be like the cool kids, or because you want to, it works, and why not?
Re:Link: A fast and simple PHP Router by 127001brewer on Thursday May 29, 2014 @12:12PM (#7816934)
Let's say you are using Apache (so maybe that already means you're not one of the cool kids?), then why would you use any of the available PHP routers?

Overall, I'm not trying to discount anyone's work with developing PHP routers, but I'd like to understand the benefits of using them.

Re:Link: A fast and simple PHP Router by bartonfink on Thursday May 29, 2014 @12:27PM (#7817014)
It would help decouple your application code and structure from Apache configuration and the need to be familiar with Apache configuration syntax and nuance. Your Apache config would just need to route all requests to your app root to one routing file, which is pretty simple, and then the routing file can process from there. You'd also have the benefits of more configurable routing, if you needed that (e.g. routing based on specific parameters in a POST instead of just based on the request url and method).
Re:Link: A fast and simple PHP Router by Nickoladze on Thursday May 29, 2014 @12:44PM (#7817129)
I would prefer to have all my behavior be in my codebase.

Also, restarting Apache after adding a route would get tedious during development.

Re:Link: A fast and simple PHP Router by insky on Thursday May 29, 2014 @12:47PM (#7817147)
I guess you are asking why not just use the filesystem one-one like mapping url to file?

A benefit of routing is that you can have a single point of contact in your app: a front controller. And you can place your route config in one place.

This router also has RESTful routing, detecting HTTP methods upfront that correspond to a predictable method name in a named class.

Re:Link: A fast and simple PHP Router by johnnymonster on Thursday May 29, 2014 @01:11PM (#7817282)
Do you have experience with massive http conf files? They become very hard to maintain! There is always going to be tradeoffs between maintainability, speed and ease of use. It just depends what sort of application you are running.
Re:Link: A fast and simple PHP Router by erming on Thursday May 29, 2014 @12:46PM (#7817141)
Looks like everyone and their grandma has written their own PHP router.

Here's mine:

https://github.com/erming/route

Code: https://github.com/erming/route/blob/master/route.php

~80 LOC

Re:Link: A fast and simple PHP Router by krapp on Thursday May 29, 2014 @05:21PM (#7818663)
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.

Re:Link: A fast and simple PHP Router by insky on Thursday May 29, 2014 @12:48PM (#7817150)
How does this compare with something like Slim?

+1 for being lightweight and readable.

Re:Link: A fast and simple PHP Router by darkstar999 on Thursday May 29, 2014 @12:50PM (#7817159)
Slim is a whole (micro)frameork that includes routing. This is just a request router.
Re:Link: A fast and simple PHP Router by jamesmoss on Thursday May 29, 2014 @12:29PM (#7817027)
Meh, another PHP library using lengthy static methods making it impossible to extend.
Re:Link: A fast and simple PHP Router by andrewryno on Thursday May 29, 2014 @12:48PM (#7817149)
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')`.
Re:Link: A fast and simple PHP Router by insky on Thursday May 29, 2014 @12:59PM (#7817219)
It looks tidier with the global.
Re:Link: A fast and simple PHP Router by alphadevx on Thursday May 29, 2014 @11:52AM (#7816838)
Nice work! A micro-micro-framework, makes Slim and Silex look big.
Re:Link: A fast and simple PHP Router by arenaninja on Thursday May 29, 2014 @12:22PM (#7816990)
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

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

Re:Link: A fast and simple PHP Router by apsdehal on Thursday May 29, 2014 @12:45PM (#7817136)
Thanks for your concern, I would try to implement features you have said, Its still under development mode and needs much polish.
Re:Link: A fast and simple PHP Router by arenaninja on Thursday May 29, 2014 @12:59PM (#7817218)
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
Re:Link: A fast and simple PHP Router by pothibo on Thursday May 29, 2014 @01:46PM (#7817535)
Is it a PHP thing to indent with tabs rather than 2 spaces? Genuinely curious.
Re:Link: A fast and simple PHP Router by uses on Thursday May 29, 2014 @02:03PM (#7817639)
The community-organized PSR-2 standard [1], used by every major framework and open-source package, states:

> Code MUST use 4 spaces for indenting, not tabs

[1] http://www.php-fig.org/psr/psr-2/

Re:Link: A fast and simple PHP Router by mjburgess on Thursday May 29, 2014 @01:46PM (#7817538)
Nope, it's a ruby thing I believe.
Re:Link: A fast and simple PHP Router by leeoniya on Thursday May 29, 2014 @01:50PM (#7817561)
or maybe it's a javascript thing http://code.jquery.com/jquery-2.1.1.js

or maybe it's just a trolling thing.

Re:Link: A fast and simple PHP Router by darkstar999 on Thursday May 29, 2014 @12:49PM (#7817154)
No tests?
Re:Link: A fast and simple PHP Router by apsdehal on Thursday May 29, 2014 @01:14PM (#7817305)
Its under development, much to be added. Thanks for your concern.
Re:Link: A fast and simple PHP Router by pearjuice on Thursday May 29, 2014 @01:21PM (#7817349)
It's written in PHP.
Re:Link: A fast and simple PHP Router by nawariata on Thursday May 29, 2014 @01:53PM (#7817581)
Your nick should read prejudice.
Re:Link: A fast and simple PHP Router by ceejayoz on Thursday May 29, 2014 @01:46PM (#7817539)
Which is readily unit tested.
Re:Link: A fast and simple PHP Router by darkstar999 on Thursday May 29, 2014 @05:07PM (#7818592)
Yes because no other PHP projects have perfectly good unit tests. /s