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?
Routing is what happens when an application determines which controllers and actions are executed based on the URL requested. Simply put, it is how the framework gets from http://localhost/users/list.html to the Users controller and the list() action.
Routing provides you with libraries of electrical, piping and tubing parts. You can model additional parts and add them to the libraries. You can find selections of pre-defined parts in the Routing Library, and also in the Design Library.
The Router attempts to match the first one, or two, parts of the path component to a corresponding route combination ( Controller / Action [method], or just a Controller that executes a default action (method). An action, or command, is simply a method off of a specific Controller .
Try Klein:
Single file, standalone and robust:
"klein.php is a lightning fast router for PHP 5.3+"
https://github.com/chriso/klein.php
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With