PHP code example of n1215 / http-router
1. Go to this page and download the library: Download n1215/http-router library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
n1215 / http-router example snippets
// 1. Implement RouterInterface. You can use RoutingResultFactory concrete classes to create RoutingResult.
class YourRouter implements N1215\Http\Router\RouterInterface
{
public function match(ServerRequestInterface $request) : RoutingResultInterface
{
// implement
}
}
// 2. Implement RoutingErrorResponderInterface.
class YourRoutingErrorResponder implements N1215\Http\Router\Handler\RoutingErrorResponderInterface
{
public function supports(RoutingException $exception): bool
{
// implement
}
public function respond(RoutingException $exception, ServerRequestInterface $request): ResponseInterface
{
// implement
}
}
// 3. Configure to inject them into RoutingHandler.
$routingHandler = new N1215\Http\Router\Handler\RoutingHandler(
new YourRouter(),
new YourNotFoundErrorResponder(),
new YourMethodNotAllowedErrorResponder()
);
// 4. Use RoutingHandler as an implementation of PSR-15 server request handler.
/** @var \Psr\Http\Message\ServerRequestInterface $request */
/** @var \Psr\Http\Message\ResponseInterface $response */
$response = $routingHandler->handle($request);