class PageNotFoundEventSubscriber in Apigee API Catalog 8.2
Same name and namespace in other branches
- 8 src/EventSubscriber/PageNotFoundEventSubscriber.php \Drupal\apigee_api_catalog\EventSubscriber\PageNotFoundEventSubscriber
Handles not found exceptions for apidoc entities.
@package Drupal\apigee_api_catalog\EventSubscriber
Hierarchy
- class \Drupal\apigee_api_catalog\EventSubscriber\PageNotFoundEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of PageNotFoundEventSubscriber
1 string reference to 'PageNotFoundEventSubscriber'
1 service uses PageNotFoundEventSubscriber
File
- src/
EventSubscriber/ PageNotFoundEventSubscriber.php, line 36
Namespace
Drupal\apigee_api_catalog\EventSubscriberView source
class PageNotFoundEventSubscriber implements EventSubscriberInterface {
/**
* The path validator service.
*
* @var \Drupal\Core\Path\PathValidatorInterface
*/
protected $pathValidator;
/**
* The patch matcher service.
*
* @var \Drupal\Core\Path\PathMatcherInterface
*/
protected $pathMatcher;
/**
* PageNotFoundEventSubscriber constructor.
*
* @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
* The patch matcher service.
* @param \Drupal\Core\Path\PathValidatorInterface $path_validator
* The path validator service.
*/
public function __construct(PathMatcherInterface $path_matcher, PathValidatorInterface $path_validator) {
$this->pathValidator = $path_validator;
$this->pathMatcher = $path_matcher;
}
/**
* Redirects to the apidoc canonical route if we have a not found exception.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The exception event.
*/
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));
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[KernelEvents::EXCEPTION][] = [
'onNotFoundException',
0,
];
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PageNotFoundEventSubscriber:: |
protected | property | The patch matcher service. | |
PageNotFoundEventSubscriber:: |
protected | property | The path validator service. | |
PageNotFoundEventSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
PageNotFoundEventSubscriber:: |
public | function | Redirects to the apidoc canonical route if we have a not found exception. | |
PageNotFoundEventSubscriber:: |
public | function | PageNotFoundEventSubscriber constructor. |