PHP code example of alanvdb / middleware-dispatcher
1. Go to this page and download the library: Download alanvdb/middleware-dispatcher 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/ */
alanvdb / middleware-dispatcher example snippets
lanVdb\Dispatcher\Dispatcher;
use AlanVdb\Dispatcher\Factory\DispatcherFactory;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use GuzzleHttp\Psr7\ServerRequest;
use GuzzleHttp\Psr7\Response;
class ExampleMiddleware implements MiddlewareInterface
{
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
// Add your middleware logic here
return $handler->handle($request);
}
}
$middlewares = [new ExampleMiddleware()];
$factory = new DispatcherFactory();
$dispatcher = $factory->createDispatcher($middlewares);
$request = new ServerRequest('GET', 'https://api.example.com/data');
$response = $dispatcher->handle($request);
echo $response->getBody();