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 93afb42

Browse filesBrowse files
bug #51704 [Routing] Fix routing collection defaults when adding a new route to a collection (bram123)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [Routing] Fix routing collection defaults when adding a new route to a collection | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | ~ | License | MIT | Doc PR | ~ In our current Symfony project we use PHP routing files, and use routing collections that are setup before adding routes. Before upgrading to Symfony 6.3 this meant we could add an attribute to the collection, add X routes and they would all have this routing attribute (be it stateless / methods / defaults). After upgrading to S6.3 this no longer works. PR #49517 removed the createRoute method from the CollectionConfigurator, because it was thought to be unused, but this method was used when adding a new Route to a RouteCollection. Without the method, the createRoute method of the LocalizedRouteTrait was used. This means that when setting up a route collection before adding routes, the new routes no longer get the collection settings. Added a new unittest to prevent this from happening again. For now only added to the PhpFileLoaderTest, I don't know if this is also possible for XML/YML routing config to also add a test in those LoaderTests. Commits ------- bfdd16e [Routing] Fix routing collection defaults when adding a new route to a collection
2 parents 0552d19 + bfdd16e commit 93afb42
Copy full SHA for 93afb42

File tree

3 files changed

+54
-0
lines changed
Filter options

3 files changed

+54
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Loader/Configurator/CollectionConfigurator.php
+8Lines 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
}
+21Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
+25Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.