class MethodFilter

Same name in other branches
  1. 9 core/lib/Drupal/Core/Routing/MethodFilter.php \Drupal\Core\Routing\MethodFilter
  2. 10 core/lib/Drupal/Core/Routing/MethodFilter.php \Drupal\Core\Routing\MethodFilter
  3. 11.x core/lib/Drupal/Core/Routing/MethodFilter.php \Drupal\Core\Routing\MethodFilter

Filters routes based on the HTTP method.

Hierarchy

Expanded class hierarchy of MethodFilter

1 file declares its use of MethodFilter
MethodFilterTest.php in core/tests/Drupal/Tests/Core/Routing/MethodFilterTest.php
1 string reference to 'MethodFilter'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses MethodFilter
method_filter in core/core.services.yml
Drupal\Core\Routing\MethodFilter

File

core/lib/Drupal/Core/Routing/MethodFilter.php, line 12

Namespace

Drupal\Core\Routing
View source
class MethodFilter implements FilterInterface {
    
    /**
     * {@inheritdoc}
     */
    public function filter(RouteCollection $collection, Request $request) {
        $method = $request->getMethod();
        $all_supported_methods = [];
        foreach ($collection->all() as $name => $route) {
            $supported_methods = $route->getMethods();
            // A route not restricted to specific methods allows any method. If this
            // is the case, we'll also have at least one route left in the collection,
            // hence we don't need to calculate the set of all supported methods.
            if (empty($supported_methods)) {
                continue;
            }
            // If the GET method is allowed we also need to allow the HEAD method
            // since HEAD is a GET method that doesn't return the body.
            if (in_array('GET', $supported_methods, TRUE)) {
                $supported_methods[] = 'HEAD';
            }
            if (!in_array($method, $supported_methods, TRUE)) {
                $all_supported_methods = array_merge($supported_methods, $all_supported_methods);
                $collection->remove($name);
            }
        }
        if (count($collection)) {
            return $collection;
        }
        throw new MethodNotAllowedException(array_unique($all_supported_methods));
    }

}

Members

Title Sort descending Modifiers Object type Summary
MethodFilter::filter public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.