public addPrefix ( string $prefix, array $defaults = [], array $requirements = [], array $options = [] ) | ||
$prefix | string | An optional prefix to add before each pattern of the route collection |
$defaults | array | An array of default values |
$requirements | array | An array of requirements |
$options | array | An array of options |
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);
}
}
}
/** * Adds a route collection to the current set of routes (at the end of the current set). * * @param RouteCollection $collection A RouteCollection instance * @param string $prefix An optional prefix to add before each pattern of the route collection */ public function addCollection(RouteCollection $collection, $prefix = '') { $collection->addPrefix($prefix); foreach ($collection->getResources() as $resource) { $this->addResource($resource); } $this->routes = array_merge($this->routes, $collection->all()); }