class HttpMethodMatcher

This class filters routes based on their HTTP Method.

Hierarchy

Expanded class hierarchy of HttpMethodMatcher

3 files declare their use of HttpMethodMatcher
FirstEntryFinalMatcherTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Routing/FirstEntryFinalMatcherTest.php
Definition of Drupal\system\Tests\Routing\NestedMatcherTest.
HttpMethodMatcherTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Routing/HttpMethodMatcherTest.php
Definition of Drupal\system\Tests\Routing\HttpMethodMMatcherTest.
NestedMatcherTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Routing/NestedMatcherTest.php
Definition of Drupal\system\Tests\Routing\NestedMatcherTest.

File

drupal/core/lib/Drupal/Core/Routing/HttpMethodMatcher.php, line 17
Definition of Drupal\Core\Routing\HttpMethodMatcher.

Namespace

Drupal\Core\Routing
View source
class HttpMethodMatcher extends PartialMatcher {

  /**
   * Matches a request against multiple routes.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   A Request object against which to match.
   *
   * @return \Symfony\Component\Routing\RouteCollection
   *   A RouteCollection of matched routes.
   */
  public function matchRequestPartial(Request $request) {
    $possible_methods = array();
    $method = $request
      ->getMethod();
    $collection = new RouteCollection();
    foreach ($this->routes
      ->all() as $name => $route) {

      // _method could be a |-delimited list of allowed methods, or null. If
      // null, we accept any method.
      $allowed_methods = array_filter(explode('|', strtoupper($route
        ->getRequirement('_method'))));
      if (empty($allowed_methods) || in_array($method, $allowed_methods)) {
        $collection
          ->add($name, $route);
      }
      else {

        // Build a list of methods that would have matched. Note that we only
        // need to do this if a route doesn't match, because if even one route
        // passes then we'll never throw the exception that needs this array.
        $possible_methods += $allowed_methods;
      }
    }
    if (!count($collection
      ->all())) {
      throw new MethodNotAllowedException(array_unique($possible_methods));
    }
    return $collection;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
HttpMethodMatcher::matchRequestPartial public function Matches a request against multiple routes. Overrides PartialMatcherInterface::matchRequestPartial
PartialMatcher::$routes protected property The RouteCollection this matcher should match against.
PartialMatcher::setCollection public function Sets the route collection this matcher should use. Overrides PartialMatcherInterface::setCollection