I am writing a class that handles the routing of my PHP web service, but I need to fix the regex and I want to know what would be the most efficient way of parsing the url.
Example URL:
- POST / users
- Get / users
- GET / users & limit = 10 & offset = 0
- GET / users / search & keyword = Richard
- GET / users / 15 / posts / 38
What I want to create in PHP for the class is:
$router = new Router(); $router->addRoute('POST', '/users', function(){}); $router->addRoute('GET', '/users/:uid/posts/:pid', function($uid, $pid){}); $target = $router->doRouting();
The target variable will now contain an array with:
This is what I got so far:
class Router{ use Singleton; private $routes = []; private $routeCount = 0; public function addRoute($method, $url, $callback){ $this->routes[] = ['method' => $method, 'url' => $url, 'callback' => $callback]; $this->routeCount++; } public function doRouting(){ $reqUrl = $_SERVER['REQUEST_URI']; $reqMet = $_SERVER['REQUEST_METHOD']; for($i = 0; $i < $this->routeCount; $i++){
So I need a regular expression, which is first of all:
- / mainAction /: ArgumentName / secondaryAction /: secondaryActionName
checks if this matches $ reqUrl (see for loop above)
- Retrieves the arguments, so we can use them in our callback function.
What I tried myself:
(code should be in the for loop @ doRouting function) // Extract arguments ... $this->routing[$i]['url'] = str_replace(':arg', '.+', $this->routing[$i]['url']); // Does the url matches the routing url? if(preg_match('#^' . $this->routes[$i]['url'] . '$#', $reqUrl)){ return $this->routes[$i]; }
I really appreciate all the help, thank you very much.
php regex routing
onlineracoon
source share