如何在EasyAdmin菜单的URL中添加Twig链接?
The issue you're facing is that EasyAdmin's YAML configuration file (easy_admin.yml) doesn't parse Twig template syntax—so your attempt to reference a Twig file with {{ path() }} won't work as expected. The url field in the menu configuration expects a static URL string, a route reference, or an expression, not Twig code. Here's how to fix it properly:
Solution 1: Use Route Names Directly (Recommended)
EasyAdmin supports defining menu items using Symfony route names and parameters directly in the YAML config. This is the cleanest approach for your language switcher:
Update your easy_admin.yml menu configuration like this:
design: menu: # Your existing menu items - ... - { label: 'Language', icon: language, children: [ { label: FR, route: 'setlocale', route_params: { language: 'fr' } }, { label: EN, route: 'setlocale', route_params: { language: 'en' } } ] }
Why this works:
- The
routefield tells EasyAdmin to use your existingsetlocaleSymfony route. route_paramspasses the requiredlanguageparameter to the route.- EasyAdmin will automatically generate the correct URL for each language option—no Twig files needed.
Solution 2: Use Symfony Expression Language (For Dynamic Scenarios)
If you need more dynamic logic (e.g., conditional URLs), you can use Symfony's expression language in the YAML config. This lets you call the path() function directly:
design: menu: - ... - { label: 'Language', icon: language, children: [ { label: FR, url: "@=path('setlocale', {language: 'fr'})" }, { label: EN, url: "@=path('setlocale', {language: 'en'})" } ] }
The @= prefix tells Symfony to evaluate the string as an expression, which will resolve to the correct route URL.
Why Your Original Setup Failed
Your previous approach pointed the menu URL to a Twig file (easy_admin/Language/fr.html.twig). When clicked, this would load the Twig file, which only outputs the route path as plain text—not redirect to that route. The menu item needs to point directly to the route's URL, not a template that displays it.
内容的提问来源于stack exchange,提问作者Paul

