You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在EasyAdmin菜单的URL中添加Twig链接?

Fixing Language Switch Menu in EasyAdmin (YAML Configuration)

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:

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 route field tells EasyAdmin to use your existing setlocale Symfony route.
  • route_params passes the required language parameter 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:44:38