Skip to content

Instantly share code, notes, and snippets.

@nanoninja
Last active May 28, 2022 16:41
Show Gist options
  • Save nanoninja/ea884f983bcfddd0d2b5 to your computer and use it in GitHub Desktop.
Save nanoninja/ea884f983bcfddd0d2b5 to your computer and use it in GitHub Desktop.
Simple Regex Router
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