Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Routing options #6738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
UPGRADE FROM 2.x to 3.0
=======================

### Routing

* Some route settings have been renamed:

* The `pattern` setting for a route has been deprecated in favor of `path`
* The `_scheme` and `_method` requirements have been moved to the `schemes` and `methods` settings

Before:

```
article_edit:
pattern: /article/{id}
requirements: { '_method': 'POST|PUT', '_scheme': 'https', 'id': '\d+' }

<route id="article_edit" pattern="/article/{id}">
<requirement key="_method">POST|PUT</requirement>
<requirement key="_scheme">https</requirement>
<requirement key="id">\d+</requirement>
</route>

$route = new Route();
$route->setPattern('/article/{id}');
$route->setRequirement('_method', 'POST|PUT');
$route->setRequirement('_scheme', 'https');
```

After:

```
article_edit:
path: /article/{id}
methods: [POST, PUT]
schemes: https
requirements: { 'id': '\d+' }

<route id="article_edit" pattern="/article/{id}" methods="POST PUT" schemes="https">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be path here as it is the after example right?

<requirement key="id">\d+</requirement>
</route>

$route = new Route();
$route->setPath('/article/{id}');
$route->setMethods(array('POST', 'PUT'));
$route->setSchemes('https');
```
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function outputRoutes(OutputInterface $output, $routes = null)
? implode(', ', $requirements['_method']) : $requirements['_method']
)
: 'ANY';
$hostname = '' !== $route->getHostnamePattern() ? $route->getHostnamePattern() : 'ANY';
$hostname = '' !== $route->getHostname() ? $route->getHostname() : 'ANY';
$maxName = max($maxName, strlen($name));
$maxMethod = max($maxMethod, strlen($method));
$maxHostname = max($maxHostname, strlen($hostname));
Expand All @@ -109,7 +109,7 @@ protected function outputRoutes(OutputInterface $output, $routes = null)
? implode(', ', $requirements['_method']) : $requirements['_method']
)
: 'ANY';
$hostname = '' !== $route->getHostnamePattern() ? $route->getHostnamePattern() : 'ANY';
$hostname = '' !== $route->getHostname() ? $route->getHostname() : 'ANY';
$output->writeln(sprintf($format, $name, $method, $hostname, $route->getPattern()));
}
}
Expand All @@ -124,14 +124,14 @@ protected function outputRoute(OutputInterface $output, $name)
throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name));
}

$hostname = '' !== $route->getHostnamePattern() ? $route->getHostnamePattern() : 'ANY';
$hostname = '' !== $route->getHostname() ? $route->getHostname() : 'ANY';

$output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name)));

$output->writeln(sprintf('<comment>Name</comment> %s', $name));
$output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPattern()));
$output->writeln(sprintf('<comment>HostnamePattern</comment> %s', $hostname));
$output->writeln(sprintf('<comment>Class</comment> %s', get_class($route)));
$output->writeln(sprintf('<comment>Name</comment> %s', $name));
$output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPattern()));
$output->writeln(sprintf('<comment>Hostname</comment> %s', $hostname));
$output->writeln(sprintf('<comment>Class</comment> %s', get_class($route)));

$defaults = '';
$d = $route->getDefaults();
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function warmUp($cacheDir)
* - the route defaults,
* - the route requirements,
* - the route pattern.
* - the route hostnamePattern.
* - the route hostname.
*
* @param RouteCollection $collection
*/
Expand All @@ -94,7 +94,7 @@ private function resolveParameters(RouteCollection $collection)
}

$route->setPattern($this->resolve($route->getPattern()));
$route->setHostnamePattern($this->resolve($route->getHostnamePattern()));
$route->setHostname($this->resolve($route->getHostname()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ public function testPatternPlaceholders()
);
}

public function testHostnamePatternPlaceholders()
public function testHostnamePlaceholders()
{
$routes = new RouteCollection();

$route = new Route('foo');
$route->setHostnamePattern('/before/%parameter.foo%/after/%%unescaped%%');
$route->setHostname('/before/%parameter.foo%/after/%%unescaped%%');

$routes->add('foo', $route);

Expand All @@ -136,7 +136,7 @@ public function testHostnamePatternPlaceholders()

$this->assertEquals(
'/before/foo/after/%unescaped%',
$route->getHostnamePattern()
$route->getHostname()
);
}

Expand Down
58 changes: 49 additions & 9 deletions src/Symfony/Component/Routing/Annotation/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
*/
class Route
{
private $pattern;
private $path;
private $name;
private $requirements;
private $options;
private $defaults;
private $hostnamePattern;
private $hostname;
private $methods;
private $schemes;

/**
* Constructor.
Expand All @@ -37,9 +39,11 @@ public function __construct(array $data)
$this->requirements = array();
$this->options = array();
$this->defaults = array();
$this->methods = array();
$this->schemes = array();

if (isset($data['value'])) {
$data['pattern'] = $data['value'];
$data['path'] = $data['value'];
unset($data['value']);
}

Expand All @@ -52,24 +56,40 @@ public function __construct(array $data)
}
}

/**
* @deprecated Deprecated in 2.2, to be removed in 3.0. Use setPath instead.
*/
public function setPattern($pattern)
{
$this->pattern = $pattern;
$this->path = $pattern;
}

/**
* @deprecated Deprecated in 2.2, to be removed in 3.0. Use getPath instead.
*/
public function getPattern()
{
return $this->pattern;
return $this->path;
}

public function setHostnamePattern($pattern)
public function setPath($path)
{
$this->hostnamePattern = $pattern;
$this->path = $path;
}

public function getHostnamePattern()
public function getPath()
{
return $this->hostnamePattern;
return $this->path;
}

public function setHostname($pattern)
{
$this->hostname = $pattern;
}

public function getHostname()
{
return $this->hostname;
}

public function setName($name)
Expand Down Expand Up @@ -111,4 +131,24 @@ public function getDefaults()
{
return $this->defaults;
}

public function setSchemes($schemes)
{
$this->schemes = is_array($schemes) ? $schemes : array($schemes);
}

public function getSchemes()
{
return $this->schemes;
}

public function setMethods($methods)
{
$this->methods = is_array($methods) ? $methods : array($methods);
}

public function getMethods()
{
return $this->methods;
}
}
43 changes: 43 additions & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,49 @@ CHANGELOG
2.2.0
-----

* [DEPRECATION] Several route settings have been renamed (the old ones will be removed in 3.0):

* The `pattern` setting for a route has been deprecated in favor of `path`
* The `_scheme` and `_method` requirements have been moved to the `schemes` and `methods` settings

Before:

```
article_edit:
pattern: /article/{id}
requirements: { '_method': 'POST|PUT', '_scheme': 'https', 'id': '\d+' }

<route id="article_edit" pattern="/article/{id}">
<requirement key="_method">POST|PUT</requirement>
<requirement key="_scheme">https</requirement>
<requirement key="id">\d+</requirement>
</route>

$route = new Route();
$route->setPattern('/article/{id}');
$route->setRequirement('_method', 'POST|PUT');
$route->setRequirement('_scheme', 'https');
```

After:

```
article_edit:
path: /article/{id}
methods: [POST, PUT]
schemes: https
requirements: { 'id': '\d+' }

<route id="article_edit" pattern="/article/{id}" methods="POST PUT" schemes="https">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be path and not pattern here aswell

<requirement key="id">\d+</requirement>
</route>

$route = new Route();
$route->setPath('/article/{id}');
$route->setMethods(array('POST', 'PUT'));
$route->setSchemes('https');
```

* [BC BREAK] RouteCollection does not behave like a tree structure anymore but as
a flat array of Routes. So when using PHP to build the RouteCollection, you must
make sure to add routes to the sub-collection before adding it to the parent
Expand Down
48 changes: 32 additions & 16 deletions src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
* The @Route annotation can be set on the class (for global parameters),
* and on each method.
*
* The @Route annotation main value is the route pattern. The annotation also
* recognizes three parameters: requirements, options, and name. The name parameter
* is mandatory. Here is an example of how you should be able to use it:
* The @Route annotation main value is the route path. The annotation also
* recognizes several parameters: requirements, options, defaults, schemes,
* methods, hostname, and name. The name parameter is mandatory.
* Here is an example of how you should be able to use it:
*
* /**
* * @Route("/Blog")
Expand Down Expand Up @@ -108,11 +109,13 @@ public function load($class, $type = null)
}

$globals = array(
'pattern' => '',
'requirements' => array(),
'options' => array(),
'defaults' => array(),
'hostname_pattern' => '',
'path' => '',
'requirements' => array(),
'options' => array(),
'defaults' => array(),
'schemes' => array(),
'methods' => array(),
'hostname' => '',
);

$class = new \ReflectionClass($class);
Expand All @@ -121,8 +124,11 @@ public function load($class, $type = null)
}

if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
if (null !== $annot->getPattern()) {
$globals['pattern'] = $annot->getPattern();
// for BC reasons
if (null !== $annot->getPath()) {
$globals['path'] = $annot->getPath();
} elseif (null !== $annot->getPattern()) {
$globals['path'] = $annot->getPattern();
}

if (null !== $annot->getRequirements()) {
Expand All @@ -137,8 +143,16 @@ public function load($class, $type = null)
$globals['defaults'] = $annot->getDefaults();
}

if (null !== $annot->getHostnamePattern()) {
$globals['hostname_pattern'] = $annot->getHostnamePattern();
if (null !== $annot->getSchemes()) {
$globals['schemes'] = $annot->getSchemes();
}

if (null !== $annot->getMethods()) {
$globals['methods'] = $annot->getMethods();
}

if (null !== $annot->getHostname()) {
$globals['hostname'] = $annot->getHostname();
}
}

Expand Down Expand Up @@ -172,13 +186,15 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
}
$requirements = array_replace($globals['requirements'], $annot->getRequirements());
$options = array_replace($globals['options'], $annot->getOptions());
$schemes = array_replace($globals['schemes'], $annot->getSchemes());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

schemes and methods should also accept a single string instead of an array (same as in xml and yaml loader). this would fail currently here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

$methods = array_replace($globals['methods'], $annot->getMethods());

$hostnamePattern = $annot->getHostnamePattern();
if (null === $hostnamePattern) {
$hostnamePattern = $globals['hostname_pattern'];
$hostname = $annot->getHostname();
if (null === $hostname) {
$hostname = $globals['hostname'];
}

$route = new Route($globals['pattern'].$annot->getPattern(), $defaults, $requirements, $options, $hostnamePattern);
$route = new Route($globals['path'].$annot->getPath(), $defaults, $requirements, $options, $hostname, $schemes, $methods);

$this->configureRoute($route, $class, $method, $annot);

Expand Down
Loading