-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Routing options #6738
Changes from all commits
10183de
2834e7e
d374e70
e803f46
b357caf
5082994
65eca8a
e84cad2
9fc7def
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"> | ||
<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 |
---|---|---|
|
@@ -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"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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") | ||
|
@@ -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); | ||
|
@@ -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()) { | ||
|
@@ -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(); | ||
} | ||
} | ||
|
||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
||
|
There was a problem hiding this comment.
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?