Mercurial > hg > isophonics-drupal-site
annotate core/lib/Drupal/Core/EventSubscriber/RouteMethodSubscriber.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
rev | line source |
---|---|
Chris@0 | 1 <?php |
Chris@0 | 2 |
Chris@0 | 3 namespace Drupal\Core\EventSubscriber; |
Chris@0 | 4 |
Chris@0 | 5 use Drupal\Core\Routing\RouteBuildEvent; |
Chris@0 | 6 use Drupal\Core\Routing\RoutingEvents; |
Chris@0 | 7 use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
Chris@0 | 8 |
Chris@0 | 9 /** |
Chris@0 | 10 * Provides a default value for the HTTP method restriction on routes. |
Chris@0 | 11 * |
Chris@0 | 12 * Most routes will only deal with GET and POST requests, so we restrict them to |
Chris@0 | 13 * those two if nothing else is specified. This is necessary to give other |
Chris@0 | 14 * routes a chance during the route matching process when they are listening |
Chris@0 | 15 * for example to DELETE requests on the same path. A typical use case are REST |
Chris@0 | 16 * web service routes that use the full spectrum of HTTP methods. |
Chris@0 | 17 */ |
Chris@0 | 18 class RouteMethodSubscriber implements EventSubscriberInterface { |
Chris@0 | 19 |
Chris@0 | 20 /** |
Chris@0 | 21 * Sets a default value of GET|POST for the _method route property. |
Chris@0 | 22 * |
Chris@0 | 23 * @param \Drupal\Core\Routing\RouteBuildEvent $event |
Chris@0 | 24 * The event containing the build routes. |
Chris@0 | 25 */ |
Chris@0 | 26 public function onRouteBuilding(RouteBuildEvent $event) { |
Chris@0 | 27 foreach ($event->getRouteCollection() as $route) { |
Chris@0 | 28 $methods = $route->getMethods(); |
Chris@0 | 29 if (empty($methods)) { |
Chris@0 | 30 $route->setMethods(['GET', 'POST']); |
Chris@0 | 31 } |
Chris@0 | 32 } |
Chris@0 | 33 } |
Chris@0 | 34 |
Chris@0 | 35 /** |
Chris@0 | 36 * {@inheritdoc} |
Chris@0 | 37 */ |
Chris@0 | 38 public static function getSubscribedEvents() { |
Chris@0 | 39 // Set a higher priority to ensure that routes get the default HTTP methods |
Chris@0 | 40 // as early as possible. |
Chris@0 | 41 $events[RoutingEvents::ALTER][] = ['onRouteBuilding', 5000]; |
Chris@0 | 42 return $events; |
Chris@0 | 43 } |
Chris@0 | 44 |
Chris@0 | 45 } |