4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing
;
15 * A Route describes a route and its parameters.
17 * @author Fabien Potencier <fabien@symfony.com>
18 * @author Tobias Schultze <http://tobion.de>
22 class Route
implements \Serializable
37 private $schemes = array();
42 private $methods = array();
47 private $defaults = array();
52 private $requirements = array();
57 private $options = array();
60 * @var null|RouteCompiler
69 * * compiler_class: A class name able to compile this route instance (RouteCompiler by default)
71 * @param string $path The path pattern to match
72 * @param array $defaults An array of default parameter values
73 * @param array $requirements An array of requirements for parameters (regexes)
74 * @param array $options An array of options
75 * @param string $host The host pattern to match
76 * @param string|array $schemes A required URI scheme or an array of restricted schemes
77 * @param string|array $methods A required HTTP method or an array of restricted methods
81 public function __construct($path, array $defaults = array(), array $requirements = array(), array $options = array(), $host = '', $schemes = array(), $methods = array())
83 $this->setPath($path);
84 $this->setDefaults($defaults);
85 $this->setRequirements($requirements);
86 $this->setOptions($options);
87 $this->setHost($host);
88 // The conditions make sure that an initial empty $schemes/$methods does not override the corresponding requirement.
89 // They can be removed when the BC layer is removed.
91 $this->setSchemes($schemes);
94 $this->setMethods($methods);
98 public function serialize()
100 return serialize(array(
101 'path' => $this->path
,
102 'host' => $this->host
,
103 'defaults' => $this->defaults
,
104 'requirements' => $this->requirements
,
105 'options' => $this->options
,
106 'schemes' => $this->schemes
,
107 'methods' => $this->methods
,
111 public function unserialize($data)
113 $data = unserialize($data);
114 $this->path
= $data['path'];
115 $this->host
= $data['host'];
116 $this->defaults
= $data['defaults'];
117 $this->requirements
= $data['requirements'];
118 $this->options
= $data['options'];
119 $this->schemes
= $data['schemes'];
120 $this->methods
= $data['methods'];
124 * Returns the pattern for the path.
126 * @return string The pattern
128 * @deprecated Deprecated in 2.2, to be removed in 3.0. Use getPath instead.
130 public function getPattern()
136 * Sets the pattern for the path.
138 * This method implements a fluent interface.
140 * @param string $pattern The path pattern
142 * @return Route The current Route instance
144 * @deprecated Deprecated in 2.2, to be removed in 3.0. Use setPath instead.
146 public function setPattern($pattern)
148 return $this->setPath($pattern);
152 * Returns the pattern for the path.
154 * @return string The path pattern
156 public function getPath()
162 * Sets the pattern for the path.
164 * This method implements a fluent interface.
166 * @param string $pattern The path pattern
168 * @return Route The current Route instance
170 public function setPath($pattern)
172 // A pattern must start with a slash and must not have multiple slashes at the beginning because the
173 // generated path for this route would be confused with a network path, e.g. '//domain.com/path'.
174 $this->path
= '/'.ltrim(trim($pattern), '/');
175 $this->compiled
= null;
181 * Returns the pattern for the host.
183 * @return string The host pattern
185 public function getHost()
191 * Sets the pattern for the host.
193 * This method implements a fluent interface.
195 * @param string $pattern The host pattern
197 * @return Route The current Route instance
199 public function setHost($pattern)
201 $this->host
= (string) $pattern;
202 $this->compiled
= null;
208 * Returns the lowercased schemes this route is restricted to.
209 * So an empty array means that any scheme is allowed.
211 * @return array The schemes
213 public function getSchemes()
215 return $this->schemes
;
219 * Sets the schemes (e.g. 'https') this route is restricted to.
220 * So an empty array means that any scheme is allowed.
222 * This method implements a fluent interface.
224 * @param string|array $schemes The scheme or an array of schemes
226 * @return Route The current Route instance
228 public function setSchemes($schemes)
230 $this->schemes
= array_map('strtolower', (array) $schemes);
232 // this is to keep BC and will be removed in a future version
233 if ($this->schemes
) {
234 $this->requirements
['_scheme'] = implode('|', $this->schemes
);
236 unset($this->requirements
['_scheme']);
239 $this->compiled
= null;
245 * Returns the uppercased HTTP methods this route is restricted to.
246 * So an empty array means that any method is allowed.
248 * @return array The schemes
250 public function getMethods()
252 return $this->methods
;
256 * Sets the HTTP methods (e.g. 'POST') this route is restricted to.
257 * So an empty array means that any method is allowed.
259 * This method implements a fluent interface.
261 * @param string|array $methods The method or an array of methods
263 * @return Route The current Route instance
265 public function setMethods($methods)
267 $this->methods
= array_map('strtoupper', (array) $methods);
269 // this is to keep BC and will be removed in a future version
270 if ($this->methods
) {
271 $this->requirements
['_method'] = implode('|', $this->methods
);
273 unset($this->requirements
['_method']);
276 $this->compiled
= null;
282 * Returns the options.
284 * @return array The options
286 public function getOptions()
288 return $this->options
;
294 * This method implements a fluent interface.
296 * @param array $options The options
298 * @return Route The current Route instance
300 public function setOptions(array $options)
302 $this->options
= array(
303 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
306 return $this->addOptions($options);
312 * This method implements a fluent interface.
314 * @param array $options The options
316 * @return Route The current Route instance
318 public function addOptions(array $options)
320 foreach ($options as $name => $option) {
321 $this->options
[$name] = $option;
323 $this->compiled
= null;
329 * Sets an option value.
331 * This method implements a fluent interface.
333 * @param string $name An option name
334 * @param mixed $value The option value
336 * @return Route The current Route instance
340 public function setOption($name, $value)
342 $this->options
[$name] = $value;
343 $this->compiled
= null;
349 * Get an option value.
351 * @param string $name An option name
353 * @return mixed The option value or null when not given
355 public function getOption($name)
357 return isset($this->options
[$name]) ? $this->options
[$name] : null;
361 * Checks if an option has been set
363 * @param string $name An option name
365 * @return Boolean true if the option is set, false otherwise
367 public function hasOption($name)
369 return array_key_exists($name, $this->options
);
373 * Returns the defaults.
375 * @return array The defaults
377 public function getDefaults()
379 return $this->defaults
;
385 * This method implements a fluent interface.
387 * @param array $defaults The defaults
389 * @return Route The current Route instance
391 public function setDefaults(array $defaults)
393 $this->defaults
= array();
395 return $this->addDefaults($defaults);
401 * This method implements a fluent interface.
403 * @param array $defaults The defaults
405 * @return Route The current Route instance
407 public function addDefaults(array $defaults)
409 foreach ($defaults as $name => $default) {
410 $this->defaults
[$name] = $default;
412 $this->compiled
= null;
418 * Gets a default value.
420 * @param string $name A variable name
422 * @return mixed The default value or null when not given
424 public function getDefault($name)
426 return isset($this->defaults
[$name]) ? $this->defaults
[$name] : null;
430 * Checks if a default value is set for the given variable.
432 * @param string $name A variable name
434 * @return Boolean true if the default value is set, false otherwise
436 public function hasDefault($name)
438 return array_key_exists($name, $this->defaults
);
442 * Sets a default value.
444 * @param string $name A variable name
445 * @param mixed $default The default value
447 * @return Route The current Route instance
451 public function setDefault($name, $default)
453 $this->defaults
[$name] = $default;
454 $this->compiled
= null;
460 * Returns the requirements.
462 * @return array The requirements
464 public function getRequirements()
466 return $this->requirements
;
470 * Sets the requirements.
472 * This method implements a fluent interface.
474 * @param array $requirements The requirements
476 * @return Route The current Route instance
478 public function setRequirements(array $requirements)
480 $this->requirements
= array();
482 return $this->addRequirements($requirements);
488 * This method implements a fluent interface.
490 * @param array $requirements The requirements
492 * @return Route The current Route instance
494 public function addRequirements(array $requirements)
496 foreach ($requirements as $key => $regex) {
497 $this->requirements
[$key] = $this->sanitizeRequirement($key, $regex);
499 $this->compiled
= null;
505 * Returns the requirement for the given key.
507 * @param string $key The key
509 * @return string|null The regex or null when not given
511 public function getRequirement($key)
513 return isset($this->requirements
[$key]) ? $this->requirements
[$key] : null;
517 * Checks if a requirement is set for the given key.
519 * @param string $key A variable name
521 * @return Boolean true if a requirement is specified, false otherwise
523 public function hasRequirement($key)
525 return array_key_exists($key, $this->requirements
);
529 * Sets a requirement for the given key.
531 * @param string $key The key
532 * @param string $regex The regex
534 * @return Route The current Route instance
538 public function setRequirement($key, $regex)
540 $this->requirements
[$key] = $this->sanitizeRequirement($key, $regex);
541 $this->compiled
= null;
547 * Compiles the route.
549 * @return CompiledRoute A CompiledRoute instance
551 * @throws \LogicException If the Route cannot be compiled because the
552 * path or host pattern is invalid
554 * @see RouteCompiler which is responsible for the compilation process
556 public function compile()
558 if (null !== $this->compiled
) {
559 return $this->compiled
;
562 $class = $this->getOption('compiler_class');
564 return $this->compiled
= $class::compile($this);
567 private function sanitizeRequirement($key, $regex)
569 if (!is_string($regex)) {
570 throw new \
InvalidArgumentException(sprintf('Routing requirement for "%s" must be a string.', $key));
573 if ('' !== $regex && '^' === $regex[0]) {
574 $regex = (string) substr($regex, 1); // returns false for a single character
577 if ('$' === substr($regex, -1)) {
578 $regex = substr($regex, 0, -1);
582 throw new \InvalidArgumentException(sprintf('Routing requirement
for "%s" cannot be
empty.', $key));
585 // this is to keep BC and will be removed in a future version
586 if ('_scheme
' === $key) {
587 $this->setSchemes(explode('|', $regex));
588 } elseif ('_method
' === $key) {
589 $this->setMethods(explode('|', $regex));