8000 [Routing] added the possibility to define a prefix for all routes of … · symfony/symfony@00d959c · GitHub
[go: up one dir, main page]

Skip to content

Commit 00d959c

Browse files
committed
[Routing] added the possibility to define a prefix for all routes of a controller
1 parent b9fc357 commit 00d959c

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/Symfony/Component/Routing/CHANGELOG.md

Lines 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

Lines 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

Lines 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