diff --git a/src/Symfony/Component/Routing/CHANGELOG.md b/src/Symfony/Component/Routing/CHANGELOG.md index 33faeb73aa607..3785709d42425 100644 --- a/src/Symfony/Component/Routing/CHANGELOG.md +++ b/src/Symfony/Component/Routing/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 3.4.0 ----- + * Added the possibility to define a prefix for all routes of a controller via @Route(name="prefix_") * Added support for prioritized routing loaders. * Add matched and default parameters to redirect responses * Added support for a `controller` keyword for configuring route controllers in YAML and XML configurations. diff --git a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php index c91a7f7d5b148..e4e5306dcbb5c 100644 --- a/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php +++ b/src/Symfony/Component/Routing/Loader/AnnotationClassLoader.php @@ -129,6 +129,7 @@ public function load($class, $type = null) if (0 === $collection->count() && $class->hasMethod('__invoke') && $annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) { $globals['path'] = ''; + $globals['name'] = ''; $this->addRoute($collection, $annot, $globals, $class, $class->getMethod('__invoke')); } @@ -141,6 +142,7 @@ protected function addRoute(RouteCollection $collection, $annot, $globals, \Refl if (null === $name) { $name = $this->getDefaultRouteName($class, $method); } + $name = $globals['name'].$name; $defaults = array_replace($globals['defaults'], $annot->getDefaults()); foreach ($method->getParameters() as $param) { @@ -222,9 +224,14 @@ protected function getGlobals(\ReflectionClass $class) 'methods' => array(), 'host' => '', 'condition' => '', + 'name' => '', ); if ($annot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass)) { + if (null !== $annot->getName()) { + $globals['name'] = $annot->getName(); + } + if (null !== $annot->getPath()) { $globals['path'] = $annot->getPath(); } diff --git a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php index bf2ab4ac9cbc5..70db1ccd9ad6a 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/AnnotationClassLoaderTest.php @@ -149,6 +149,7 @@ public function testLoad($className, $routeData = array(), $methodArgs = array() public function testClassRouteLoad() { $classRouteData = array( + 'name' => 'prefix_', 'path' => '/prefix', 'schemes' => array('https'), 'methods' => array('GET'), @@ -173,7 +174,7 @@ public function testClassRouteLoad() ; $routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass'); - $route = $routeCollection->get($methodRouteData['name']); + $route = $routeCollection->get($classRouteData['name'].$methodRouteData['name']); $this->assertSame($classRouteData['path'].$methodRouteData['path'], $route->getPath(), '->load concatenates class and method route path'); $this->assertEquals(array_merge($classRouteData['schemes'], $methodRouteData['schemes']), $route->getSchemes(), '->load merges class and method route schemes'); @@ -240,7 +241,7 @@ public function testInvokableClassWithMethodRouteLoad() $this->assertNull($route, '->load ignores class route'); - $route = $routeCollection->get($methodRouteData['name']); + $route = $routeCollection->get($classRouteData['name'].$methodRouteData['name']); $this->assertSame($classRouteData['path'].$methodRouteData['path'], $route->getPath(), '->load concatenates class and method route path'); $this->assertEquals(array_merge($classRouteData['schemes'], $methodRouteData['schemes']), $route->getSchemes(), '->load merges class and method route schemes');