New in Symfony 5.4: Route Aliasing

Contributed by Jérôme Parmentier in #38464.

When refactoring applications, especially if you work on a legacy application, it's common to need to change the name of some routes. Some renames might be considered cosmetic (e.g. rename admin_dashboard to admin) but others are essential for the new application because they change the route name pattern entirely (e.g. rename api_register_customer_requests_post_collection to sylius_shop_api_register). The need to rename routes is increasingly popular among Symfony developers and that's why in Symfony 5.4, we're improving the Routing component to allow aliasing routes. In any route configuration, add the alias option and provide the name of the route which is equivalent to it. For example:

    1

2 3 4 5 6 7 8 9

config/routes.yaml

this is the existing and 'legacy' route

admin_dashboard:

...

this is the new route that replaces the old one

admin: alias: admin_dashboard

...

The admin route is considered by Symfony the same as admin_dashboard and you can generate the same URLs with both of them. The above example uses YAML, but it works with all the other route configuration formats as well (PHP, XML).

Deprecating Routes An added bonus of this feature is that you can deprecate routes and tell users in advance so they can update their applications. Imagine that you publish a third-party bundle that includes a route called user_collection_index and you want to rename it to users_index. Instead of just renaming the route (which would break all applications using the bundle) define the old route as an alias of the new route and add to it the deprecated option:

    1

2 3 4 5 6 7 8 9 10

config/routes.yaml

users_index:

...

user_collection_index: alias: 'users_index' deprecated: package: 'some-vendor/some-package' version: '3.14'

...

If any application uses the user_collectionindex route to generate URLs, they'll see the following deprecation message: "Since some-vendor/some-package 3.14: The "user_collectionindex" route alias is deprecated. You should stop using it, as it will be removed in the future." Add the message option under deprecated if you want to customize the deprecation message entirely.

                Sponsor the Symfony project.

http://feedproxy.google.com/~r/symfony/blog/~3/EfT_bp2dO8Y/new-in-symfony-5-4-route-aliasing

Vytvořeno 4y | 9. 11. 2021 8:20:10


Chcete-li přidat komentář, přihlaste se

Ostatní příspěvky v této skupině

New in Symfony 7.3: Invokable Commands and Input Attributes

This is the first article in a series showcasing the most important new features introduced by Symfony 7.3, which will be released at the end of May 2025.

22. 4. 2025 9:10:36 | Symfony
A Week of Symfony #955 (April 14–20, 2025)

This week, the upcoming Symfony 7.3 version improved the AsAlias attribute by adding a new argument, introduced Clock support for UriSigner, and refined the return type of the ContainerInterface::get(

20. 4. 2025 8:30:06 | Symfony
SymfonyOnline June 2025: Rethinking File Handling in Symfony

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

16. 4. 2025 16:30:02 | Symfony
SymfonyLive Paris 2025: Recap and replay!

SymfonyLive Paris 2025 took place three weeks ago — a big thank you to everyone who joined us! The conference was held entirely in French, and now you can relive the best moments: replays, hig

15. 4. 2025 14:50:24 | Symfony
A Week of Symfony #954 (April 7–13, 2025)

This week, Symfony 7.3 entered its "feature freeze" period in preparation for its release at the end of May 2025. Development activity focused on refining and polishing its new features, including a n

13. 4. 2025 9:40:02 | Symfony
SymfonyOnline June 2025: What's New in Symfony 7.3

SymfonyOnline June 2025 is almost here, starting in almost 2 months on:

June 10-11: Workshop days. It is possible to attend 1 two-day training or 2 one-day trainings. June 12-13: Online confe

10. 4. 2025 16:40:34 | Symfony
SymfonyCon Amsterdam 2025: Last days to enjoy early bird tickets!

The international Symfony conference of the year, SymfonyCon Amsterdam 2025, will take place in the Netherlands on November 27 & 28, 2025!

⏳ Early bird registration ends this Wednesday! D

8. 4. 2025 9:10:13 | Symfony