public function run() { $app = $this->xtr->getApplication(); /** @var $matcher RedirectableUrlMatcher */ $context = $app['request_context']; $matched = false; $path = $this->xtr->getRequest()->getPathInfo(); $lastException = null; // matching fallback route groups: foreach ($this->xtr['routes'] as $fallback => $routes) { $routes = RouteCollection::FromArray($routes)->flat(); $matcher = new RedirectableUrlMatcher($routes->toSilexCollection(), $context); try { $matcher->match($path); $matched = $fallback; break; } catch (ResourceNotFoundException $e) { // not found: silent continue $lastException = $e; } } if ($matched === false) { throw $lastException; } // configuring distinct service: $allow = [$matched]; $fallbacks = array_flip(array_keys($this->xtr['routes'])); unset($fallbacks[$matched]); $deny = array_keys($fallbacks); $this->xtr['@distinct.settings'] = ['allow' => $allow, 'deny' => $deny]; }
public function match($pathinfo) { try { return parent::match($pathinfo); } catch (ResourceNotFoundException $e) { if (!in_array($this->context->getMethod(), array('HEAD', 'GET'))) { throw $e; } } // Try matching the route with trailing slash if ('/' !== substr($pathinfo, -1)) { try { return parent::match($pathinfo . '/'); } catch (ResourceNotFoundException $e2) { throw $e; } } // Try matching the route without trailing slash $withoutTrailingSlash = substr($pathinfo, 0, -1); try { return parent::match($withoutTrailingSlash); } catch (ResourceNotFoundException $e2) { throw $e; } }
public function match($pathinfo) { try { return parent::match($pathinfo); } catch (ResourceNotFoundException $e) { return $this->matcher->match($pathinfo); } }
public function match($pathinfo) { try { return parent::match($pathinfo); } catch (ResourceNotFoundException $e) { if ('/' !== substr($pathinfo, -1)) { throw $e; } // Try matching the route without trailing slash $withoutTrailingSlash = substr($pathinfo, 0, -1); try { parent::match($withoutTrailingSlash); return $this->redirect($withoutTrailingSlash, null); } catch (ResourceNotFoundException $e2) { throw $e; } } }
public function match($pathinfo) { try { return parent::match($pathinfo); } catch (ResourceNotFoundException $e) { if ('/' !== substr($pathinfo, -1)) { throw $e; } // Try matching the route without trailing slash $withoutTrailingSlash = substr($pathinfo, 0, -1); try { parent::match($withoutTrailingSlash); return $this->redirect($withoutTrailingSlash, null); } catch (ResourceNotFoundException $e2) { // We don't care about the new exception as we are just trying // to match different versions of the route, if it fails then we // throw the original one throw $e; } } }
/** * Tries to match a URL path with a set of routes. * * If the matcher can not find information, it must throw one of the exceptions documented * below. * * @api * * @param string $pathinfo The path info to be parsed (raw format, i.e. not urldecoded) * * @return array An array of parameters */ public function match($pathinfo) { $matcher = new RedirectableUrlMatcher($this->getRouteCollection(), $this->getContext()); return $matcher->match($pathinfo); }
/** * Handles onKernelRequest events. */ public function onKernelRequest(KernelEvent $event) { $this['request'] = $event->getRequest(); $this['request_context'] = new RequestContext($this['request']->getBaseUrl(), $this['request']->getMethod(), $this['request']->getHost(), $this['request']->getScheme(), !$this['request']->isSecure() ? $this['request']->getPort() : $this['request.http_port'], $this['request']->isSecure() ? $this['request']->getPort() : $this['request.https_port']); $this['controllers']->flush(); $matcher = new RedirectableUrlMatcher($this['routes'], $this['request_context']); try { $attributes = $matcher->match($this['request']->getPathInfo()); $this['request']->attributes->add($attributes); } catch (RoutingException $e) { // make sure onSilexBefore event is dispatched $this['dispatcher']->dispatch(SilexEvents::BEFORE); if ($e instanceof ResourceNotFoundException) { $message = sprintf('No route found for "%s %s"', $this['request']->getMethod(), $this['request']->getPathInfo()); throw new NotFoundHttpException($message, $e); } else { if ($e instanceof MethodNotAllowedException) { $message = sprintf('No route found for "%s %s": Method Not Allowed (Allow: %s)', $this['request']->getMethod(), $this['request']->getPathInfo(), strtoupper(implode(', ', $e->getAllowedMethods()))); throw new MethodNotAllowedHttpException($e->getAllowedMethods(), $message, $e); } } throw $e; } $this['dispatcher']->dispatch(SilexEvents::BEFORE); }