Uses the router to route the incoming request, injecting the request
with:
- the route result object (under a key named for the RouteResult class)
- attributes for each matched routing parameter
On completion, it calls on the next middleware (typically the
dispatchMiddleware()).
If routing fails, $next() is called; if routing fails due to HTTP
method negotiation, the response is set to a 405, injected with an
Allow header, and $next() is called with its $error argument set
to the value 405 (invoking the next error middleware).
public routeMiddleware ( Psr\Http\Message\ServerRequestInterface $request, Psr\Http\Message\ResponseInterface $response, callable $next ) : Psr\Http\Message\ResponseInterface | ||
$request | Psr\Http\Message\ServerRequestInterface | |
$response | Psr\Http\Message\ResponseInterface | |
$next | callable | |
return | Psr\Http\Message\ResponseInterface |
/** * @dataProvider routerAdapters * @group 74 */ public function testWithOnlyRootPathRouteDefinedRoutingToSubPathsShouldReturn404($adapter) { $app = new Application(new $adapter()); $app->route('/', function ($req, $res, $next) { $res->getBody()->write('Middleware'); return $res; }, ['GET']); $next = function ($req, $res) { return $res; }; $request = new ServerRequest(['REQUEST_METHOD' => 'GET'], [], '/foo', 'GET'); $response = new Response(); $result = $app->routeMiddleware($request, $response, $next); $this->assertInstanceOf(Response::class, $result); $this->assertNotEquals(405, $result->getStatusCode()); }
public function testRoutingSuccessResolvingToInvokableMiddlewareCallsIt() { $request = new ServerRequest(); $response = new Response(); $result = RouteResult::fromRouteMatch('/foo', __NAMESPACE__ . '\\TestAsset\\InvokableMiddleware', []); $this->router->match($request)->willReturn($result); // No container for this one, to ensure we marshal only a potential object instance. $app = new Application($this->router->reveal()); $next = function ($request, $response) { $this->fail('Should not enter $next'); }; $test = $app->routeMiddleware($request, $response, $next); $this->assertInstanceOf('Psr\\Http\\Message\\ResponseInterface', $test); $this->assertTrue($test->hasHeader('X-Invoked')); $this->assertEquals(__NAMESPACE__ . '\\TestAsset\\InvokableMiddleware', $test->getHeaderLine('X-Invoked')); }