[Routing] added the possibility to define a prefix for all routes of a controller

This commit is contained in:
Fabien Potencier 2017-08-29 14:51:48 -07:00
parent b9fc357d1c
commit 00d959c933
3 changed files with 11 additions and 2 deletions

View File

@ -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.

View File

@ -129,6 +129,7 @@ abstract class AnnotationClassLoader implements LoaderInterface
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 @@ abstract class AnnotationClassLoader implements LoaderInterface
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 @@ abstract class AnnotationClassLoader implements LoaderInterface
'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();
}

View File

@ -149,6 +149,7 @@ class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
public function testClassRouteLoad()
{
$classRouteData = array(
'name' => 'prefix_',
'path' => '/prefix',
'schemes' => array('https'),
'methods' => array('GET'),
@ -173,7 +174,7 @@ class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
;
$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 @@ class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
$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');