Contributed by
Jules Pietri
in #30501.
Redirections to other URLs and routes allow Symfony applications evolve without breaking any existing links. These redirections are configured in YAML, XML or PHP files instead of annotations, and the resulting config was a bit verbose.
In Symfony 5.1, we've improved it to make the config much more concise. The
following examples show the changes for the config/routes.yaml
file, but
the improvement is very similar for XML and PHP config files.
Redirecting to another route:
1 2 3 4 5 6 7 8 9 10 11 12 13 | # Before
doc_shortcut:
path: /doc
controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController
defaults:
route: 'doc_page'
permanent: true
# After
doc_shortcut:
path: /doc
redirect_to_route: 'doc_page'
permanent: true
|
Redirecting to a URL:
1 2 3 4 5 6 7 8 9 10 11 12 13 | # Before
legacy_doc:
path: /legacy/doc
controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController
defaults:
path: 'https://legacy.example.com/doc'
permanent: false
# After
legacy_doc:
path: /legacy/doc
redirect_to_url: 'https://legacy.example.com/doc'
permanent: false
|
We've also improved the config of routes that render a template directly, without executing any of your controller actions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # Before
page_privacy:
path: /site/privacy-policy
controller: Symfony\Bundle\FrameworkBundle\Controller\TemplateController
defaults:
template: 'static/privacy.html.twig'
context:
site_name: 'ACME'
# After
page_privacy:
path: /site/privacy-policy
template: 'static/privacy.html.twig'
context:
site_name: 'ACME'
|
Finally, in Symfony 5.1 you can also define that a URL is gone, temporarily
(404
status) or permanently (410
status) with the gone
option:
1 2 3 4 | conference:
path: /foo-conference
gone: true
permanent: true
|
What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...