Zend\Expressive\Application::routeMiddleware PHP Method

routeMiddleware() public method

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
    public function routeMiddleware(ServerRequestInterface $request, ResponseInterface $response, callable $next)
    {
        $result = $this->router->match($request);
        if ($result->isFailure()) {
            if ($result->isMethodFailure()) {
                $response = $response->withStatus(405)->withHeader('Allow', implode(',', $result->getAllowedMethods()));
                return $next($request, $response, 405);
            }
            return $next($request, $response);
        }
        // Inject the actual route result, as well as individual matched parameters.
        $request = $request->withAttribute(Router\RouteResult::class, $result);
        foreach ($result->getMatchedParams() as $param => $value) {
            $request = $request->withAttribute($param, $value);
        }
        return $next($request, $response);
    }

Usage Example

 /**
  * @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());
 }
All Usage Examples Of Zend\Expressive\Application::routeMiddleware