Mercurial > hg > isophonics-drupal-site
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\EventSubscriber; | |
4 | |
5 use Drupal\Core\Routing\RouteBuildEvent; | |
6 use Drupal\Core\Routing\RoutingEvents; | |
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
8 | |
9 /** | |
10 * Provides a default value for the HTTP method restriction on routes. | |
11 * | |
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. | |
17 */ | |
18 class RouteMethodSubscriber implements EventSubscriberInterface { | |
19 | |
20 /** | |
21 * Sets a default value of GET|POST for the _method route property. | |
22 * | |
23 * @param \Drupal\Core\Routing\RouteBuildEvent $event | |
24 * The event containing the build routes. | |
25 */ | |
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']); | |
31 } | |
32 } | |
33 } | |
34 | |
35 /** | |
36 * {@inheritdoc} | |
37 */ | |
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]; | |
42 return $events; | |
43 } | |
44 | |
45 } |