-
Notifications
You must be signed in to change notification settings - Fork 0
Thingston HTTP Router
Pedro Brazão Ferreira edited this page Jun 16, 2022
·
2 revisions
An HTTP router supporting PSR-15 request handlers.
use Thingston\Http\Router\RequestHandlerResolver;
use Thingston\Http\Router\Route;
use Thingston\Http\Router\Router;
$router = new Router();
$router->addRoute(new Route(['GET'], '/', 'home', 'HomeHandler'))
->addRoute(new Route(['GET'], '/about', 'about', 'AboutHandler'))
->addRoute(new Route(['GET'], '/hello/{name}', 'hello', 'HelloHandler'));
/** @var Psr\Http\Message\ServerRequestInterface $request */
$matchedRoute = $router->match($request);
$resolver = new RequestHandlerResolver();
/** @var Psr\Http\Server\RequestHandlerInterface $requestHandler */
$requestHandler = $resolver->resolve($matchedRoute);
/** @var Psr\Http\Message\ResponseInterface $response */
$response = $requestHandler->handle($request);Like in the example above the RequestHandlerResolver always resolves a Route handler as an instance of Psr\Http\Server\RequestHandlerInterface which can be invoked to handle a Server Request instance.
However a Route Handler can be defined in some different ways:
- An instance of
Psr\Http\Server\RequestHandlerInterfacealready; - Any PHP
callable; - A string representing the FQNS of an invokable class;
- A string representing the FQNS of any class and its method name separated by
@(e.gApp\Action\MyAction@index); - Any string representing a Container key which returns an invokable class instance.