Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit adc862a

Browse filesBrowse files
committed
feature #24031 [Routing] Add the possibility to define a prefix for all routes of a controller (fabpot)
This PR was merged into the 3.4 branch. Discussion ---------- [Routing] Add the possibility to define a prefix for all routes of a controller | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | sensiolabs/SensioFrameworkExtraBundle#283 | License | MIT | Doc PR | not yet This PR adds the possibility to define a name on a `@Route` annotation set on a controller class. The name is then used as a prefix for all routes defined in the controller. Commits ------- 00d959c [Routing] added the possibility to define a prefix for all routes of a controller
2 parents 1730cff + 00d959c commit adc862a
Copy full SHA for adc862a

File tree

3 files changed

+11
-2
lines changed
Filter options

3 files changed

+11
-2
lines changed

‎src/Symfony/Component/Routing/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
3.4.0
55
-----
66

7+
* Added the possibility to define a prefix for all routes of a controller via @Route(name="prefix_")
78
* Added support for prioritized routing loaders.
89
* Add matched and default parameters to redirect responses
910
* Added support for a `controller` keyword for configuring route controllers in YAML and XML configurations.

‎src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ public function load($class, $type = null)
129129

130130
if (0 === $collection->count() && $class->hasMethod('__invoke') && $annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
131131
$globals['path'] = '';
132+
$globals['name'] = '';
132133
$this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke'));
133134
}
134135

@@ -141,6 +142,7 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl
141142
if (null === $name) {
142143
$name = $this->getDefaultRouteName($class, $method);
143144
}
145+
$name = $globals['name'].$name;
144146

145147
$defaults = array_replace($globals['defaults'], $annot->getDefaults());
146148
foreach ($method->getParameters() as $param) {
@@ -222,9 +224,14 @@ protected function getGlobals(\ReflectionClass $class)
222224
'methods' => array(),
223225
'host' => '',
224226
'condition' => '',
227+
'name' => '',
225228
);
226229

227230
if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) {
231+
if (null !== $annot->getName()) {
232+
$globals['name'] = $annot->getName();
233+
}
234+
228235
if (null !== $annot->getPath()) {
229236
$globals['path'] = $annot->getPath();
230237
}

‎src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public function testLoad($className, $routeData = array(), $methodArgs = array()
149149
public function testClassRouteLoad()
150150
{
151151
$classRouteData = array(
152+
'name' => 'prefix_',
152153
'path' => '/prefix',
153154
'schemes' => array('https'),
154155
'methods' => array('GET'),
@@ -173,7 +174,7 @@ public function testClassRouteLoad()
173174
;
174175

175176
$routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass');
176-
$route = $routeCollection->get($methodRouteData['name']);
177+
$route = $routeCollection->get($classRouteData['name'].$methodRouteData['name']);
177178

178179
$this->assertSame($classRouteData['path'].$methodRouteData['path'], $route->getPath(), '->load concatenates class and method route path');
179180
$this->assertEquals(array_merge($classRouteData['schemes'], $methodRouteData['schemes']), $route->getSchemes(), '->load merges class and method route schemes');
@@ -240,7 +241,7 @@ public function testInvokableClassWithMethodRouteLoad()
240241

241242
$this->assertNull($route, '->load ignores class route');
242243

243-
$route = $routeCollection->get($methodRouteData['name']);
244+
$route = $routeCollection->get($classRouteData['name'].$methodRouteData['name']);
244245

245246
$this->assertSame($classRouteData['path'].$methodRouteData['path'], $route->getPath(), '->load concatenates class and method route path');
246247
$this->assertEquals(array_merge($classRouteData['schemes'], $methodRouteData['schemes']), $route->getSchemes(), '->load merges class and method route schemes');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.