/** * Process an incoming request and/or response. * * Accepts a server-side request and a response instance, and does * something with them. * * If the response is not complete and/or further processing would not * interfere with the work done in the middleware, or if the middleware * wants to delegate to another process, it can use the `$out` callable * if present. * * If the middleware does not return a value, execution of the current * request is considered complete, and the response instance provided will * be considered the response to return. * * Alternately, the middleware may return a response instance. * * Often, middleware will `return $out();`, with the assumption that a * later middleware will return a response. * * @param Request $request * @param Response $response * @param null|callable $out * @return null|Response */ public function __invoke(Request $request, Response $response, callable $out = null) { $matchedRoute = $this->router->match($request); $params = $matchedRoute->getMatchedParams(); // Determine the language to use based on the lang parameter $lang = isset($params['lang']) ? $params['lang'] : 'en'; $this->translator->setLocale($lang); return $out($request, $response); }
/** * @param Request $request * @return array [] */ public function getOptionsForRequest(ServerRequestInterface $request) { /** * @var RouteResult $routeMatch */ $routePath = $this->router->match($request)->getMatchedRouteName(); foreach ($this->config as $route) { if ($route['path'] === $routePath) { return isset($route['options']) ? $route['options'] : []; } } return []; }
public function testInvalidIdRequest() { $request = $this->request->withUri(new Uri('http://localhost/api/ping/one'))->withMethod('GET'); /** @var \Zend\Expressive\Router\RouteResult $result */ $result = $this->router->match($request); $this->assertTrue($result->isFailure()); }
/** * Middleware that routes the incoming request and delegates to the matched middleware. * * Uses the router to route the incoming request, dispatching matched * middleware on a request success condition. * * 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). * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * @return ResponseInterface * @throws Exception\InvalidArgumentException if the route result does not contain middleware * @throws Exception\InvalidArgumentException if unable to retrieve middleware from the container * @throws Exception\InvalidArgumentException if unable to resolve middleware to a callable */ 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); } foreach ($result->getMatchedParams() as $param => $value) { $request = $request->withAttribute($param, $value); } $middleware = $result->getMatchedMiddleware(); if (!$middleware) { throw new Exception\InvalidMiddlewareException(sprintf('The route %s does not have a middleware to dispatch', $result->getMatchedRouteName())); } if (is_callable($middleware)) { return $middleware($request, $response, $next); } if (!is_string($middleware)) { throw new Exception\InvalidMiddlewareException('The middleware specified is not callable'); } // try to get the action name from the container (if exists) $callable = $this->marshalMiddlewareFromContainer($middleware); if (is_callable($callable)) { return $callable($request, $response, $next); } // try to instantiate the middleware directly, if possible $callable = $this->marshalInvokableMiddleware($middleware); if (!is_callable($callable)) { throw new Exception\InvalidMiddlewareException(sprintf('Unable to resolve middleware "%s" to a callable', $middleware)); } return $callable($request, $response, $next); }
/** * Middleware that routes the incoming request and delegates to the matched middleware. * * Uses the router to route the incoming request, dispatching matched * middleware on a request success condition. * * 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). * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * @return ResponseInterface * @throws Exception\InvalidArgumentException if the route result does not contain middleware * @throws Exception\InvalidArgumentException if unable to retrieve middleware from the container * @throws Exception\InvalidArgumentException if unable to resolve middleware to a callable */ public function routeMiddleware(ServerRequestInterface $request, ResponseInterface $response, callable $next) { $result = $this->router->match($request); $this->notifyRouteResultObservers($result); 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); } $middleware = $result->getMatchedMiddleware(); if (!$middleware) { throw new Exception\InvalidMiddlewareException(sprintf('The route %s does not have a middleware to dispatch', $result->getMatchedRouteName())); } if (is_array($middleware) && !is_callable($middleware)) { $middlewarePipe = $this->marshalMiddlewarePipe($middleware); return $middlewarePipe($request, $response, $next); } $callable = $this->marshalMiddleware($middleware); return $callable($request, $response, $next); }
/** * Process an incoming request and/or response. * * Accepts a server-side request and a response instance, and does * something with them. * * If the response is not complete and/or further processing would not * interfere with the work done in the middleware, or if the middleware * wants to delegate to another process, it can use the `$out` callable * if present. * * If the middleware does not return a value, execution of the current * request is considered complete, and the response instance provided will * be considered the response to return. * * Alternately, the middleware may return a response instance. * * Often, middleware will `return $out();`, with the assumption that a * later middleware will return a response. * * @param Request $request * @param Response $response * @param null|callable $out * @return null|Response */ public function __invoke(Request $request, Response $response, callable $out = null) : Response { $currentRoute = $this->router->match($request); $currentRoutePath = $request->getUri()->getPath(); // If current route is a success route and it has been previously cached, write cached content and retur if ($currentRoute->isSuccess() && $this->cache->contains($currentRoutePath)) { $response->getBody()->write($this->cache->fetch($currentRoutePath)); return $response; } // If the response is not cached, process the next middleware and get its response, then cache it $resp = $out($request, $response); if ($resp instanceof Response && $this->isResponseCacheable($resp, $currentRoute->getMatchedParams())) { $this->cache->save($currentRoutePath, $resp->getBody()->__toString()); } return $resp; }
/** * Middleware that routes the incoming request and delegates to the matched middleware. * * 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). * * @param ServerRequestInterface $request * @param ResponseInterface $response * @param callable $next * @return 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); }
/** * @return RouteResult */ public function getCurrentRouteResult() { return $this->router->match($this->request); }
/** * @param ServerRequestInterface $request * @param RouterInterface $router * @return array */ private function getParsedAttributes(ServerRequestInterface $request, RouterInterface $router) { $routeResult = $router->match($request); return $routeResult->getMatchedParams(); }
/** * {@inheritDoc} */ public function collectParams(ServerRequestInterface $request) { return $this->router->match($request)->getMatchedParams(); }