src/EventSubscriber/DisabledRouteSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-07-15
  5.  * Time: 20:57
  6.  */
  7. namespace App\EventSubscriber;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. class DisabledRouteSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * Feature key in route defaults
  16.      */
  17.     const DISABLED_ROUTE_KEY '_route_disabled';
  18.     /**
  19.      * Filter by routing metadata
  20.      *
  21.      * @throws NotFoundHttpException
  22.      */
  23.     public function onKernelController(ControllerEvent $event): void
  24.     {
  25.         if (!$event->getRequest()->attributes->has(self::DISABLED_ROUTE_KEY)) {
  26.             return;
  27.         }
  28.         if (true === $event->getRequest()->attributes->get(self::DISABLED_ROUTE_KEY)) {
  29.             throw new NotFoundHttpException('Route in this app is disabled.');
  30.         }
  31.     }
  32.     /**
  33.      * @inheritDoc
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             KernelEvents::CONTROLLER => 'onKernelController',
  39.         ];
  40.     }
  41. }