Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an standalone PHP routing library? [closed]

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?

like image 294
abeger Avatar asked Mar 13 '13 17:03

abeger


People also ask

What is php routing?

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.

What is a routing library?

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.

What is routing in MVC php?

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 .


2 Answers

Try Klein:

Single file, standalone and robust:

"klein.php is a lightning fast router for PHP 5.3+"

  • Flexible regular expression routing (inspired by Sinatra)
  • A set of boilerplate methods for rapidly building web apps
  • Almost no overhead => 2500+ requests/second

https://github.com/chriso/klein.php

like image 147
Kartik Avatar answered Oct 07 '22 20:10

Kartik


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:

  • FastRoute https://github.com/nikic/FastRoute - Written by Nikita Popov
  • Phroute https://github.com/mrjgreen/phroute - Written by Joe Green

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.

like image 28
DaGhostman Dimitrov Avatar answered Oct 07 '22 21:10

DaGhostman Dimitrov