use FastRoute\RouteCollector; use FastRoute\Dispatcher; $router = new RouteCollector(); $router->addRoute('GET', '/user/{id:\d+}', 'UsersController@show'); $router->addRoute('POST', '/user', 'UsersController@store'); $router->addRoute('PUT', '/user/{id:\d+}', 'UsersController@update'); $router->addRoute('DELETE', '/user/{id:\d+}', 'UsersController@destroy'); $dispatcher = new Dispatcher\GroupCountBased($router->getData()); $uri = $_SERVER['REQUEST_URI']; $httpMethod = $_SERVER['REQUEST_METHOD']; $routeInfo = $dispatcher->dispatch($httpMethod, $uri); switch ($routeInfo[0]) { case Dispatcher::NOT_FOUND: // handle 404 Not Found error break; case Dispatcher::METHOD_NOT_ALLOWED: // handle 405 Method Not Allowed error break; case Dispatcher::FOUND: $handler = $routeInfo[1]; $vars = $routeInfo[2]; [$controller, $method] = explode('@', $handler); call_user_func_array([new $controller, $method], $vars); break; }
use League\Route\Router; $router = new Router(); $router->map('GET', '/user/{id:\d+}', 'UsersController@show'); $router->map('POST', '/user', 'UsersController@store'); $router->map('PUT', '/user/{id:\d+}', 'UsersController@update'); $router->map('DELETE', '/user/{id:\d+}', 'UsersController@destroy'); $dispatcher = $router->getDispatcher(); $uri = $_SERVER['REQUEST_URI']; $httpMethod = $_SERVER['REQUEST_METHOD']; $response = $dispatcher->dispatch($httpMethod, $uri); $response->send();Example 2 shows how to define routes for handling HTTP requests using the League\Route package. The routes are registered with the `Router` and the `Dispatcher` is used for matching the incoming requests and sending the response. The `$response` variable returned by the `Dispatcher` is then sent to the client. Based on these code examples, the PHP Router Assembly package library is likely a routing library for PHP that supports multiple routing patterns, including regular expressions, and provides a consistent interface for matching incoming HTTP requests with their corresponding controller and method. It also provides a middleware system for intercepting and modifying requests and responses.