Inject locale's requirements in your Symfony routes
Published on 2019-08-01 • Modified on 2019-08-01
This is a feature that was introduced in Symfony 4.1. It allows to directly inject a parameter in a route requirement instead of putting raw values. In this case, for this website, the locales_requirements
parameter contains %locale%|fr
, where locale
is the default locale "en". When you want to add a locale to your project, all you will have to do is to add it to this parameter. (and translate everything of course 😁). Note that this annotation is declared at the controller level, therefore all actions declared in this one inherit from those requirements.
#[Route(path: '/{_locale}/blog', name: 'blog_', requirements: ['_locale' => '%locales_requirements%'])]
final class BlogController extends AbstractController
{
More on Stackoverflow Random snippet
Call to action
Did you like this post? You can help me back in several ways: (use the "reply" link on the right to comment or to contact me )
- Report any error/typo.
- Report something that could be improved.
- Like and repost!
- Follow me on Bluesky 🦋
- Subscribe to the RSS feed.
- Click on the More on Stackoverflow buttons to make me win "Announcer" badges 🏅.
Thank you for reading! And see you soon on Strangebuzz! 😉