6 ms·
Link: A fast and simple PHP Router
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.
- alphadevx 12y agoNice work! A micro-micro-framework, makes Slim and Silex look big.
- arenaninja 12y agoIt 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 12y agoThanks for your concern, I would try to implement features you have said, Its still under development mode and needs much polish.
- arenaninja 12y agoGood 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
- leeoniya 12y agothere 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.html http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Router....
- nubs 12y agoThis list is missing https://github.com/nikic/FastRoute https://github.com/nikic/FastRoute which is definitely worth looking at.
- ams6110 12y agoI used Epiphany last time I needed something like this. https://github.com/jmathai/epiphany https://github.com/jmathai/epiphany Note that is more than a router, it's a microframework.
- krapp 12y agoWhat 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.)
- Nickoladze 12y agoDont forget µ which is totally a legit router https://github.com/lastguest/mu https://github.com/lastguest/mu
- 127001brewer 12y agoWhy would you want to use any PHP router instead of writing the rewrite rules within an Apache VirtualHost file or a .htaccess file?
- RobAley 12y agoAt 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?
- 127001brewer 12y agoLet'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.
- bartonfink 12y agoIt 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).
- Nickoladze 12y agoI would prefer to have all my behavior be in my codebase. Also, restarting Apache after adding a route would get tedious during development.
- insky 12y agoI 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.
- nubs 12y agoThe 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-regular-expressions.html http://nikic.github.io/2014/02/18/Fast-request-routing-using...
- apsdehal 12y agoThanks for your concern, this router is a result out of a effort to learn about routing. I will try to add benchmarks soon.
- jamesmoss 12y agoMeh, another PHP library using lengthy static methods making it impossible to extend.
- andrewryno 12y agoYeah 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 12y agoIt looks tidier with the global.
- deleted 12y ago[deleted]
- erming 12y agoLooks 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 12y agoI 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 12y agoHow does this compare with something like Slim? +1 for being lightweight and readable.
- darkstar999 12y agoSlim is a whole (micro)frameork that includes routing. This is just a request router.
- darkstar999 12y agoNo tests?
- apsdehal 12y agoIts under development, much to be added. Thanks for your concern.
- pearjuice 12y agoIt's written in PHP.
- ceejayoz 12y agoWhich is readily unit tested.
- nawariata 12y agoYour nick should read prejudice.
- darkstar999 12y agoYes because no other PHP projects have perfectly good unit tests. /s
- deleted 12y ago[deleted]
- mcfunley 12y agoFWIW 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 12y agoHow 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 12y agoPut it in .htaccess. No restarts needed.
- 127001brewer 12y agoThat's correct - I was thinking of using an Apache VirtualHost file. (Isn't using the .htaccess file slower(?) than using the VirtualHost file?)
- mcfunley 12y ago
- pothibo 12y agoIs it a PHP thing to indent with tabs rather than 2 spaces? Genuinely curious.
- mjburgess 12y agoNope, it's a ruby thing I believe.
- leeoniya 12y agoor 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.
- uses 12y agoThe 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/ http://www.php-fig.org/psr/psr-2/