Link: A fast and simple PHP Router (github.com)
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.
60 comments
[ 460 ms ] story [ 4695 ms ] threadFor 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
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....
https://github.com/lastguest/mu
Edit: as in, which router do you pick and how do you pick one? Choice can lead to decision paralysis.
Although, doing it without a router is probably always going to be faster.
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.
Note that is more than a router, it's a microframework.
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.)
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.
Also, restarting Apache after adding a route would get tedious during development.
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.
[0]: https://github.com/nikic/FastRoute
[1]: http://nikic.github.io/2014/02/18/Fast-request-routing-using...
Here's mine:
https://github.com/erming/route
Code: https://github.com/erming/route/blob/master/route.php
~80 LOC
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.
+1 for being lightweight and readable.
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.)
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?
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.
or maybe it's just a trolling thing.
> Code MUST use 4 spaces for indenting, not tabs
[1] http://www.php-fig.org/psr/psr-2/