Skip to content

Commit de61f2b

Browse files
committed
Provide PHP routing files in parallel of the XML ones
Symfony has deprecated the XML format for route definitions. The bundle now ships PHP routing files in addition to the XML ones (with tests ensuring they are in sync) and the documentation uses the PHP files. The XML files will be removed in the next major version of the bundle as removing them is a BC break.
1 parent 012d3a4 commit de61f2b

9 files changed

Lines changed: 209 additions & 14 deletions

File tree

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,12 +360,12 @@ logging in, creating users, etc.
360360
361361
# app/config/routing.yml
362362
fos_user:
363-
resource: "@FOSUserBundle/Resources/config/routing/all.xml"
363+
resource: "@FOSUserBundle/Resources/config/routing/all.php"
364364
365365
.. code-block:: xml
366366
367367
<!-- app/config/routing.xml -->
368-
<import resource="@FOSUserBundle/Resources/config/routing/all.xml"/>
368+
<import resource="@FOSUserBundle/Resources/config/routing/all.php"/>
369369
370370
.. note::
371371

docs/routing.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Advanced routing configuration
22
==============================
33

4-
By default, the routing file ``@FOSUserBundle/Resources/config/routing/all.xml`` imports
4+
By default, the routing file ``@FOSUserBundle/Resources/config/routing/all.php`` imports
55
all the routing files and enables all the routes.
66
In the case you want to enable or disable the different available routes, just use the
77
single routing configuration files.
@@ -12,29 +12,29 @@ single routing configuration files.
1212
1313
# app/config/routing.yml
1414
fos_user_security:
15-
resource: "@FOSUserBundle/Resources/config/routing/security.xml"
15+
resource: "@FOSUserBundle/Resources/config/routing/security.php"
1616
1717
fos_user_profile:
18-
resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
18+
resource: "@FOSUserBundle/Resources/config/routing/profile.php"
1919
prefix: /profile
2020
2121
fos_user_register:
22-
resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
22+
resource: "@FOSUserBundle/Resources/config/routing/registration.php"
2323
prefix: /register
2424
2525
fos_user_resetting:
26-
resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
26+
resource: "@FOSUserBundle/Resources/config/routing/resetting.php"
2727
prefix: /resetting
2828
2929
fos_user_change_password:
30-
resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
30+
resource: "@FOSUserBundle/Resources/config/routing/change_password.php"
3131
prefix: /profile
3232
3333
.. code-block:: xml
3434
3535
<!-- app/config/routing.xml -->
36-
<import resource="@FOSUserBundle/Resources/config/routing/security.xml"/>
37-
<import resource="@FOSUserBundle/Resources/config/routing/profile.xml" prefix="/profile" />
38-
<import resource="@FOSUserBundle/Resources/config/routing/registration.xml" prefix="/register" />
39-
<import resource="@FOSUserBundle/Resources/config/routing/resetting.xml" prefix="/resetting" />
40-
<import resource="@FOSUserBundle/Resources/config/routing/change_password.xml" prefix="/profile" />
36+
<import resource="@FOSUserBundle/Resources/config/routing/security.php"/>
37+
<import resource="@FOSUserBundle/Resources/config/routing/profile.php" prefix="/profile" />
38+
<import resource="@FOSUserBundle/Resources/config/routing/registration.php" prefix="/register" />
39+
<import resource="@FOSUserBundle/Resources/config/routing/resetting.php" prefix="/resetting" />
40+
<import resource="@FOSUserBundle/Resources/config/routing/change_password.php" prefix="/profile" />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return Routes::config([
6+
'security' => [
7+
'resource' => '@FOSUserBundle/Resources/config/routing/security.php',
8+
],
9+
'profile' => [
10+
'resource' => '@FOSUserBundle/Resources/config/routing/profile.php',
11+
'prefix' => '/profile',
12+
],
13+
'registration' => [
14+
'resource' => '@FOSUserBundle/Resources/config/routing/registration.php',
15+
'prefix' => '/register',
16+
],
17+
'resetting' => [
18+
'resource' => '@FOSUserBundle/Resources/config/routing/resetting.php',
19+
'prefix' => '/resetting',
20+
],
21+
'change_password' => [
22+
'resource' => '@FOSUserBundle/Resources/config/routing/change_password.php',
23+
'prefix' => '/profile',
24+
],
25+
]);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return Routes::config([
6+
'fos_user_change_password' => [
7+
'path' => '/change-password',
8+
'methods' => ['GET', 'POST'],
9+
'controller' => 'fos_user.change_password.controller::changePasswordAction',
10+
],
11+
]);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return Routes::config([
6+
'fos_user_profile_show' => [
7+
'path' => '/',
8+
'methods' => ['GET'],
9+
'controller' => 'fos_user.profile.controller::showAction',
10+
],
11+
'fos_user_profile_edit' => [
12+
'path' => '/edit',
13+
'methods' => ['GET', 'POST'],
14+
'controller' => 'fos_user.profile.controller::editAction',
15+
],
16+
]);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return Routes::config([
6+
'fos_user_registration_register' => [
7+
'path' => '/',
8+
'methods' => ['GET', 'POST'],
9+
'controller' => 'fos_user.registration.controller::registerAction',
10+
],
11+
'fos_user_registration_check_email' => [
12+
'path' => '/check-email',
13+
'methods' => ['GET'],
14+
'controller' => 'fos_user.registration.controller::checkEmailAction',
15+
],
16+
'fos_user_registration_confirm' => [
17+
'path' => '/confirm/{token}',
18+
'methods' => ['GET'],
19+
'controller' => 'fos_user.registration.controller::confirmAction',
20+
],
21+
'fos_user_registration_confirmed' => [
22+
'path' => '/confirmed',
23+
'methods' => ['GET'],
24+
'controller' => 'fos_user.registration.controller::confirmedAction',
25+
],
26+
]);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return Routes::config([
6+
'fos_user_resetting_request' => [
7+
'path' => '/request',
8+
'methods' => ['GET'],
9+
'controller' => 'fos_user.resetting.controller::requestAction',
10+
],
11+
'fos_user_resetting_send_email' => [
12+
'path' => '/send-email',
13+
'methods' => ['POST'],
14+
'controller' => 'fos_user.resetting.controller::sendEmailAction',
15+
],
16+
'fos_user_resetting_check_email' => [
17+
'path' => '/check-email',
18+
'methods' => ['GET'],
19+
'controller' => 'fos_user.resetting.controller::checkEmailAction',
20+
],
21+
'fos_user_resetting_reset' => [
22+
'path' => '/reset/{token}',
23+
'methods' => ['GET', 'POST'],
24+
'controller' => 'fos_user.resetting.controller::resetAction',
25+
],
26+
]);
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 Routes::config([
6+
'fos_user_security_login' => [
7+
'path' => '/login',
8+
'methods' => ['GET', 'POST'],
9+
'controller' => 'fos_user.security.controller::loginAction',
10+
],
11+
'fos_user_security_check' => [
12+
'path' => '/login_check',
13+
'methods' => ['POST'],
14+
'controller' => 'fos_user.security.controller::checkAction',
15+
],
16+
'fos_user_security_logout' => [
17+
'path' => '/logout',
18+
'methods' => ['GET', 'POST'],
19+
'controller' => 'fos_user.security.controller::logoutAction',
20+
],
21+
]);

tests/Routing/RoutingTest.php

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\Routing\Loader\PhpFileLoader;
1617
use Symfony\Component\Routing\Loader\XmlFileLoader;
1718
use Symfony\Component\Routing\RouteCollection;
1819

@@ -27,6 +28,42 @@ class RoutingTest extends TestCase
2728
*/
2829
public function testLoadRouting($routeName, $path, array $methods)
2930
{
31+
$locator = new FileLocator();
32+
$loader = new PhpFileLoader($locator);
33+
34+
$collection = new RouteCollection();
35+
$collection->addCollection($loader->load(__DIR__.'/../../src/Resources/config/routing/change_password.php'));
36+
$subCollection = $loader->load(__DIR__.'/../../src/Resources/config/routing/profile.php');
37+
$subCollection->addPrefix('/profile');
38+
$collection->addCollection($subCollection);
39+
$subCollection = $loader->load(__DIR__.'/../../src/Resources/config/routing/registration.php');
40+
$subCollection->addPrefix('/register');
41+
$collection->addCollection($subCollection);
42+
$subCollection = $loader->load(__DIR__.'/../../src/Resources/config/routing/resetting.php');
43+
$subCollection->addPrefix('/resetting');
44+
$collection->addCollection($subCollection);
45+
$collection->addCollection($loader->load(__DIR__.'/../../src/Resources/config/routing/security.php'));
46+
47+
$route = $collection->get($routeName);
48+
$this->assertNotNull($route, sprintf('The route "%s" should exists', $routeName));
49+
$this->assertSame($path, $route->getPath());
50+
$this->assertSame($methods, $route->getMethods());
51+
}
52+
53+
/**
54+
* @dataProvider loadRoutingProvider
55+
* @group legacy
56+
*
57+
* @param string $routeName
58+
* @param string $path
59+
* @param string[] $methods
60+
*/
61+
public function testLoadRoutingLegacy($routeName, $path, array $methods)
62+
{
63+
if (!class_exists(XmlFileLoader::class)) {
64+
$this->markTestSkipped('XML routing files are not supported on Symfony 8.');
65+
}
66+
3067
$locator = new FileLocator();
3168
$loader = new XmlFileLoader($locator);
3269

@@ -52,7 +89,7 @@ public function testLoadRouting($routeName, $path, array $methods)
5289
/**
5390
* @return iterable<array{string, string, string[]}>
5491
*/
55-
public function loadRoutingProvider(): iterable
92+
public static function loadRoutingProvider(): iterable
5693
{
5794
return [
5895
['fos_user_change_password', '/change-password', ['GET', 'POST']],
@@ -75,4 +112,37 @@ public function loadRoutingProvider(): iterable
75112
['fos_user_security_logout', '/logout', ['GET', 'POST']],
76113
];
77114
}
115+
116+
/**
117+
* @dataProvider provideRouteFiles
118+
*
119+
* @group legacy
120+
*/
121+
public function testLegacyFileConsistency(string $filename): void
122+
{
123+
if (!class_exists(XmlFileLoader::class)) {
124+
$this->markTestSkipped('XML routing files are not supported on Symfony 8.');
125+
}
126+
127+
$locator = new FileLocator();
128+
129+
$phpLoader = new PhpFileLoader($locator);
130+
$xmlLoader = new XmlFileLoader($locator);
131+
132+
$phpCollection = $phpLoader->load(__DIR__.\sprintf('/../../src/Resources/config/routing/%s.php', $filename));
133+
$xmlCollection = $xmlLoader->load(__DIR__.\sprintf('/../../src/Resources/config/routing/%s.xml', $filename));
134+
135+
$this->assertEquals($phpCollection->all(), $xmlCollection->all());
136+
}
137+
138+
/**
139+
* @return iterable<array{string}>
140+
*/
141+
public static function provideRouteFiles(): iterable
142+
{
143+
yield ['profile'];
144+
yield ['registration'];
145+
yield ['resetting'];
146+
yield ['security'];
147+
}
78148
}

0 commit comments

Comments
 (0)