3 namespace Drupal\Core\EventSubscriber;
5 use Drupal\Core\Routing\RouteBuildEvent;
6 use Drupal\Core\Routing\RoutingEvents;
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10 * Provides a default value for the HTTP method restriction on routes.
12 * Most routes will only deal with GET and POST requests, so we restrict them to
13 * those two if nothing else is specified. This is necessary to give other
14 * routes a chance during the route matching process when they are listening
15 * for example to DELETE requests on the same path. A typical use case are REST
16 * web service routes that use the full spectrum of HTTP methods.
18 class RouteMethodSubscriber implements EventSubscriberInterface {
21 * Sets a default value of GET|POST for the _method route property.
23 * @param \Drupal\Core\Routing\RouteBuildEvent $event
24 * The event containing the build routes.
26 public function onRouteBuilding(RouteBuildEvent $event) {
27 foreach ($event->getRouteCollection() as $route) {
28 $methods = $route->getMethods();
29 if (empty($methods)) {
30 $route->setMethods(['GET', 'POST']);
38 public static function getSubscribedEvents() {
39 // Set a higher priority to ensure that routes get the default HTTP methods
40 // as early as possible.
41 $events[RoutingEvents::ALTER][] = ['onRouteBuilding', 5000];