Register a new route with the given verbs.
Route::get('/users/{id}', function ($id) { return 'User '.$id; });
$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) { $r->addRoute('GET', '/users/{id:\d+}', 'get_user_handler'); }); $routeInfo = $dispatcher->dispatch('GET', '/users/123'); switch ($routeInfo[0]) { case FastRoute\Dispatcher::NOT_FOUND: // ... 404 Not Found break; case FastRoute\Dispatcher::METHOD_NOT_ALLOWED: $allowedMethods = $routeInfo[1]; // ... 405 Method Not Allowed break; case FastRoute\Dispatcher::FOUND: $handler = $routeInfo[1]; $vars = $routeInfo[2]; // ... call $handler with $vars break; }
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; $collection = new RouteCollection(); $collection->add('blog_show', new Route('/blog/{slug}', array( '_controller' => 'AppBundle:Blog:show', ))); $context = new RequestContext(); $context->fromRequest($request); $matcher = new UrlMatcher($collection, $context); $params = $matcher->match('/blog/my-post'); echo $params['_controller'];Based on the examples provided, the package library used for PHP Route matching can be Laravel, FastRoute, or Symfony's Routing Component.