1. Go to this page and download the library: Download n1215/hakudo 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 / hakudo example snippets
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use N1215\Http\Router\Exception\RouteNotFoundException;
use N1215\Http\Router\Exception\RoutingException;
use N1215\Http\Router\Handler\RoutingErrorResponderInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;
// 1. Implement RoutingErrorResponderInterface.
class YourNotFoundErrorResponder implements RoutingErrorResponderInterface
{
public function supports(RoutingException $exception): bool
{
// use this responder only when Route not found exception is thrown by the router.
return $exception instanceof RouteNotFoundException;
}
public function respond(RoutingException $exception, ServerRequestInterface $request): ResponseInterface
{
// implement
}
}
// 2. Implement PSR-11 Container
class YourContainer implements Psr\Container\ContainerInterface
{
public function get($id)
{
// implement
}
public function has($id)
{
// implement
}
}
// 3. Implement PSR-15 RequestHandler
class YourHandler implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
$id = $request->getAttribute('id'); // matched route parameters becomes attributes.
// implement
}
}
// 4. Implement PSR-15 Middleware
class YourMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// implement
}
}
// 5. register handler and middleware to the container
$container = new YourContainer();
$container->bind(YourHandler::class, function () { return new YourHandler(); });
$container->bind(YourMiddleware::class, function () { return new YourMiddleware(); });
// 6. create Router instance
$router = new N1215\Hakudo\Router(
N1215\Jugoya\LazyRequestHandlerBuilder::fromContainer($container),
new N1215\Http\Router\Result\RoutingResultFactory()
);
// 7. add routes
use N1215\Hakudo\RequestMatcher\Path;
$router->add(
Path::get('|/resources/(?<resource_name>[a-z_]+)/(?<id>[0-9]+)|'), // you can use any other RequestMatcher instance.
YourHandler::class, // you can use same signature closure or RequestHandler instance instead of RequestHandler's container entry name.
[YourMiddleware::class] // you can use same signature closure or Middleware instance instead of Middleware's container entry name.
)->name('route_name');
// 8. create RoutingHandler
$routingHandler = new N1215\Http\Router\Handler\RoutingHandler(
$router,
new YourNotFoundErrorResponder()
);
// 9. Use RoutingHandler as an implementation of PSR-15 server request handler.
/** @var ServerRequestInterface $request */
/** @var ResponseInterface $response */
$response = $routingHandler->handle($request);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.