You are here

public function PageNotFoundEventSubscriber::onNotFoundException in Apigee API Catalog 8.2

Same name and namespace in other branches
  1. 8 src/EventSubscriber/PageNotFoundEventSubscriber.php \Drupal\apigee_api_catalog\EventSubscriber\PageNotFoundEventSubscriber::onNotFoundException()

Redirects to the apidoc canonical route if we have a not found exception.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event: The exception event.

File

src/EventSubscriber/PageNotFoundEventSubscriber.php, line 71

Class

PageNotFoundEventSubscriber
Handles not found exceptions for apidoc entities.

Namespace

Drupal\apigee_api_catalog\EventSubscriber

Code

public function onNotFoundException(GetResponseForExceptionEvent $event) {

  // Check if the request uri matches an apidoc canonical route.
  // Also check for apidoc valid path.
  if ($event
    ->getException() instanceof NotFoundHttpException && ($uri = $event
    ->getRequest()
    ->getRequestUri()) && $this->pathMatcher
    ->matchPath($uri, '/api/*/*') && (list(, , $id) = explode('/', $uri)) && ($path = "/api/{$id}") && $this->pathValidator
    ->isValid($path)) {

    // Redirect to the apidoc.
    $event
      ->setResponse(new RedirectResponse($path));
  }
}