public function RouteCollection::addPrefix

Adds a prefix to all routes in the current set.

@api

Parameters

string $prefix An optional prefix to add before each pattern of the route collection:

array $defaults An array of default values:

array $requirements An array of requirements:

array $options An array of options:

File

drupal/core/vendor/symfony/routing/Symfony/Component/Routing/RouteCollection.php, line 224

Class

RouteCollection
A RouteCollection represents a set of Route instances as a tree structure.

Namespace

Symfony\Component\Routing

Code

public function addPrefix($prefix, $defaults = array(), $requirements = array(), $options = array()) {

  // a prefix must not end with a slash
  $prefix = rtrim($prefix, '/');
  if ('' === $prefix && empty($defaults) && empty($requirements) && empty($options)) {
    return;
  }

  // a prefix must start with a slash
  if ('' !== $prefix && '/' !== $prefix[0]) {
    $prefix = '/' . $prefix;
  }
  $this->prefix = $prefix . $this->prefix;
  foreach ($this->routes as $route) {
    if ($route instanceof RouteCollection) {
      $route
        ->addPrefix($prefix, $defaults, $requirements, $options);
    }
    else {
      $route
        ->setPattern($prefix . $route
        ->getPattern());
      $route
        ->addDefaults($defaults);
      $route
        ->addRequirements($requirements);
      $route
        ->addOptions($options);
    }
  }
}