Last active
May 28, 2022 16:41
-
-
Save nanoninja/ea884f983bcfddd0d2b5 to your computer and use it in GitHub Desktop.
Simple Regex Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RegexRouter | |
{ | |
private $routes = array(); | |
public function addRoute($route, callable $service) | |
{ | |
$route = '#^'.$route.'$#'; | |
$this->routes[$route] = $service; | |
return $this; | |
} | |
public function isValid($uri) | |
{ | |
$params = []; | |
foreach ($this->routes as $route => $service) { | |
if (preg_match($route, $uri, $params)) { | |
array_shift($params); | |
return call_user_func_array($service, array_values($params)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment