1. Go to this page and download the library: Download makise-co/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/ */
declare(strict_types=1);
use GuzzleHttp\Psr7\Response;
use MakiseCo\Http\Router\Exception\MethodNotAllowedException;
use MakiseCo\Http\Router\Exception\RouteNotFoundException;
use MakiseCo\Middleware\ErrorHandlerInterface;
use MakiseCo\Middleware\ErrorHandlingMiddleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
// It is just an example implementation (a more proper way is to use a response factory)
class ErrorHandler implements ErrorHandlerInterface
{
public function handle(Throwable $e, ServerRequestInterface $request): ResponseInterface
{
if ($e instanceof RouteNotFoundException) {
return new Response(404, [], $e->getMessage());
}
if ($e instanceof MethodNotAllowedException) {
// following https://tools.ietf.org/html/rfc7231#section-6.5.5
return new Response(405, ['Allow' => $e->getAllowedMethods()], $e->getMessage());
}
return new Response(500, [], "Internal Server Error<br><br>{$e->getMessage()}");
}
}
$router = $collector->getRouter();
$app = (new \MakiseCo\Middleware\MiddlewarePipeFactory())->create([
new ErrorHandlingMiddleware(new ErrorHandler()), // placing error handling middleware first
$router
]);
// or
$app = new \MakiseCo\Middleware\Dispatcher([
new ErrorHandlingMiddleware(new ErrorHandler()), // placing error handling middleware first
$router
]);
// or use any other middleware dispatcher
$response = $app->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.