@@ -1340,6 +1340,23 @@ have been renamed. Let's say you have a route called ``product_show``:
1340
1340
1341
1341
.. configuration-block ::
1342
1342
1343
+ .. code-block :: php-attributes
1344
+
1345
+ // src/Controller/ProductController.php
1346
+ namespace App\Controller;
1347
+
1348
+ use Symfony\Component\HttpFoundation\Response;
1349
+ use Symfony\Component\Routing\Attribute\Route;
1350
+
1351
+ class ProductController
1352
+ {
1353
+ #[Route('/product/{id}', name: 'product_show')]
1354
+ public function show(): Response
1355
+ {
1356
+ // ...
1357
+ }
1358
+ }
1359
+
1343
1360
.. code-block :: yaml
1344
1361
1345
1362
# config/routes.yaml
@@ -1376,6 +1393,25 @@ Instead of duplicating the original route, you can create an alias for it.
1376
1393
1377
1394
.. configuration-block ::
1378
1395
1396
+ .. code-block :: php-attributes
1397
+
1398
+ // src/Controller/ProductController.php
1399
+ namespace App\Controller;
1400
+
1401
+ use Symfony\Component\HttpFoundation\Response;
1402
+ use Symfony\Component\Routing\Attribute\Route;
1403
+
1404
+ class ProductController
1405
+ {
1406
+ // "alias" named argument indicates the name of the alias you want to create.
1407
+ // The alias will point to the actual route "product_show"
1408
+ #[Route('/product/{id}', name: 'product_show', alias: ['product_details'])]
1409
+ public function show(): Response
1410
+ {
1411
+ // ...
1412
+ }
1413
+ }
1414
+
1379
1415
.. code-block :: yaml
1380
1416
1381
1417
# config/routes.yaml
@@ -1416,6 +1452,15 @@ Instead of duplicating the original route, you can create an alias for it.
1416
1452
In this example, both ``product_show `` and ``product_details `` routes can
1417
1453
be used in the application and will produce the same result.
1418
1454
1455
+ .. note ::
1456
+
1457
+ Using non-attributes formats (YAML, XML and PHP) is the only way
1458
+ to define an alias pointing to a route that you don't own.
1459
+
1460
+ So that you can use your own route name for URL generation,
1461
+ while actually using a route defined by a third-party bundle as the target of that URL generation,
1462
+ as the 2 definitions are not required to be in the same config file (or even in the same format).
1463
+
1419
1464
.. _routing-alias-deprecation :
1420
1465
1421
1466
Deprecating Route Aliases
@@ -1436,6 +1481,42 @@ This way, the ``product_show`` alias could be deprecated.
1436
1481
1437
1482
.. configuration-block ::
1438
1483
1484
+ .. code-block :: php-attributes
1485
+
1486
+ // src/Controller/ProductController.php
1487
+ namespace App\Controller;
1488
+
1489
+ use Symfony\Component\HttpFoundation\Response;
1490
+ use Symfony\Component\Routing\Attribute\Route;
1491
+
1492
+ class ProductController
1493
+ {
1494
+ // this outputs the following generic deprecation message:
1495
+ // Since acme/package 1.2: The "product_show" route alias is deprecated. You should stop using it, as it will be removed in the future.
1496
+ #[Route('/product/{id}',
1497
+ name: 'product_details',
1498
+ alias: new DeprecatedAlias(
1499
+ aliasName: 'product_show',
1500
+ package: 'acme/package',
1501
+ version: '1.2',
1502
+ ),
1503
+ )]
1504
+ // Or, you can also define a custom deprecation message (%alias_id% placeholder is available)
1505
+ #[Route('/product/{id}',
1506
+ name: 'product_details',
1507
+ alias: new DeprecatedAlias(
1508
+ aliasName: 'product_show',
1509
+ package: 'acme/package',
1510
+ version: '1.2',
1511
+ message: 'The "%alias_id%" route alias is deprecated. Please use "product_details" instead.',
1512
+ ),
1513
+ )]
1514
+ public function show(): Response
1515
+ {
1516
+ // ...
1517
+ }
1518
+ }
1519
+
1439
1520
.. code-block :: yaml
1440
1521
1441
1522
# Move the concrete route definition under ``product_details``
0 commit comments