You are here

class RoutingAlterSubscriber in Commerce Cart API 8

Removes CSRF header requirements from our routes.

Hierarchy

  • class \Drupal\commerce_cart_api\EventSubscriber\RoutingAlterSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of RoutingAlterSubscriber

1 string reference to 'RoutingAlterSubscriber'
commerce_cart_api.services.yml in ./commerce_cart_api.services.yml
commerce_cart_api.services.yml
1 service uses RoutingAlterSubscriber
commerce_cart_api.routing_alter_subscriber in ./commerce_cart_api.services.yml
Drupal\commerce_cart_api\EventSubscriber\RoutingAlterSubscriber

File

src/EventSubscriber/RoutingAlterSubscriber.php, line 12

Namespace

Drupal\commerce_cart_api\EventSubscriber
View source
class RoutingAlterSubscriber implements EventSubscriberInterface {

  /**
   * Alters our cart API routes to remove _csrf_request_header_token.
   *
   * @param \Drupal\Core\Routing\RouteBuildEvent $event
   *   The event to process.
   */
  public function onRoutingRouteAlter(RouteBuildEvent $event) {
    $route_collection = $event
      ->getRouteCollection();
    foreach ($route_collection as $route) {
      if ($route
        ->hasRequirement('_cart_api')) {
        $requirements = $route
          ->getRequirements();
        unset($requirements['_csrf_request_header_token']);
        $route
          ->setRequirements($requirements);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[RoutingEvents::ALTER][] = [
      'onRoutingRouteAlter',
      -100,
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RoutingAlterSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
RoutingAlterSubscriber::onRoutingRouteAlter public function Alters our cart API routes to remove _csrf_request_header_token.