app/Customize/EventListener/RouteBlockListener.php line 12

Open in your IDE?
  1. <?php
  2. namespace Customize\EventListener;
  3. use Symfony\Component\HttpKernel\Event\RequestEvent;
  4. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpKernel\KernelEvents;
  7. class RouteBlockListener implements EventSubscriberInterface
  8. {
  9.     public function onKernelRequest(RequestEvent $event)
  10.     {
  11.         $request $event->getRequest();
  12.         $path $request->getPathInfo();
  13.         $isOldPage env('OLD_PAGE');
  14.         if($isOldPage) return;
  15.         $blockedRoutes = [
  16.             '/cart',
  17.             '/shopping',
  18.             '/shopping/confirm',
  19.             '/shopping/checkout',
  20.             '/products/list'
  21.         ];
  22.         if (in_array($path$blockedRoutes)) {
  23.             throw new NotFoundHttpException('Page not found');
  24.         }
  25.         if (preg_match('#^/products/detail/\d+$#'$path)) {
  26.             throw new NotFoundHttpException('Page not found');
  27.         }
  28.         if (preg_match('#\/mypage\/#'$path)) {
  29.             throw new NotFoundHttpException('Page not found');
  30.         }
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             KernelEvents::REQUEST => [
  39.                 ['onKernelRequest'20],
  40.             ],
  41.         ];
  42.     }
  43. }