Skip to content

Commit bfdd16e

Browse files
bram123nicolas-grekas
authored andcommitted
[Routing] Fix routing collection defaults when adding a new route to a collection
1 parent 0552d19 commit bfdd16e

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

src/Symfony/Component/Routing/Loader/Configurator/CollectionConfigurator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,12 @@ final public function host(string|array $host): static
114114

115115
return $this;
116116
}
117+
118+
/**
119+
* This method overrides the one from LocalizedRouteTrait.
120+
*/
121+
private function createRoute(string $path): Route
122+
{
123+
return (clone $this->route)->setPath($path);
124+
}
117125
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (RoutingConfigurator $routes) {
6+
$collection = $routes->collection();
7+
$collection
8+
->methods(['GET'])
9+
->defaults(['attribute' => true])
10+
->stateless();
11+
12+
$collection->add('defaultsA', '/defaultsA')
13+
->locale('en')
14+
->format('html');
15+
16+
$collection->add('defaultsB', '/defaultsB')
17+
->methods(['POST'])
18+
->stateless(false)
19+
->locale('en')
20+
->format('html');
21+
};

src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,31 @@ public function testLoadingRouteWithDefaults()
103103
$this->assertSame('html', $defaultsRoute->getDefault('_format'));
104104
}
105105

106+
public function testLoadingRouteWithCollectionDefaults()
107+
{
108+
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']));
109+
$routes = $loader->load('collection-defaults.php');
110+
111+
$this->assertCount(2, $routes);
112+
113+
$defaultsRoute = $routes->get('defaultsA');
114+
$this->assertSame(['GET'], $defaultsRoute->getMethods());
115+
$this->assertArrayHasKey('attribute', $defaultsRoute->getDefaults());
116+
$this->assertTrue($defaultsRoute->getDefault('_stateless'));
117+
$this->assertSame('/defaultsA', $defaultsRoute->getPath());
118+
$this->assertSame('en', $defaultsRoute->getDefault('_locale'));
119+
$this->assertSame('html', $defaultsRoute->getDefault('_format'));
120+
121+
// The second route has a specific method and is not stateless, overwriting the collection settings
122+
$defaultsRoute = $routes->get('defaultsB');
123+
$this->assertSame(['POST'], $defaultsRoute->getMethods());
124+
$this->assertArrayHasKey('attribute', $defaultsRoute->getDefaults());
125+
$this->assertFalse($defaultsRoute->getDefault('_stateless'));
126+
$this->assertSame('/defaultsB', $defaultsRoute->getPath());
127+
$this->assertSame('en', $defaultsRoute->getDefault('_locale'));
128+
$this->assertSame('html', $defaultsRoute->getDefault('_format'));
129+
}
130+
106131
public function testLoadingImportedRoutesWithDefaults()
107132
{
108133
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy