I\'m attempting to use the FastRoute routing library and can\'t get the simple usage example to work.
Here is the basic usage example found on the GitHub page:
I made a demo API that uses FastRoute and PHP-DI(dependency injection) to showcase how to use both. See: https://github.com/tochix/shapes-api/blob/master/src/Service/Http/Router.php#L94. In a nutshell, it looks like this:
/**
* @param string $requestMethod
* @param string $requestUri
* @param Dispatcher $dispatcher
* @throws DependencyException
* @throws NotFoundException
*/
private function dispatch(string $requestMethod, string $requestUri, Dispatcher $dispatcher)
{
$routeInfo = $dispatcher->dispatch($requestMethod, $requestUri);
switch ($routeInfo[0]) {
case Dispatcher::NOT_FOUND:
$this->getRequest()->sendNotFoundHeader();
break;
case Dispatcher::METHOD_NOT_ALLOWED:
$this->getRequest()->sendMethodNotAllowedHeader();
break;
case Dispatcher::FOUND:
list($state, $handler, $vars) = $routeInfo;
list($class, $method) = explode(static::HANDLER_DELIMITER, $handler, 2);
$controller = $this->getContainer()->get($class);
$controller->{$method}(...array_values($vars));
unset($state);
break;
}
}