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

Symfony项目中Twig format_datetime过滤器报错求助

解决Twig format_datetime 报错:Call to undefined method Twig\Extension\CoreExtension::dateConverter()

问题根源

你的Twig依赖版本不匹配:twig/twig 为3.8.0,但twig/intl-extra和twig/extra-bundle是3.9.0。Twig扩展包版本必须与核心Twig版本严格对齐,否则会出现内部方法调用不兼容的问题。

解决步骤

  1. 统一依赖版本
    执行Composer命令,将所有Twig相关依赖锁定到同一版本(二选一即可):

    • 降级扩展包匹配核心Twig版本:
      composer require twig/twig:3.8.0 twig/intl-extra:3.8.0 twig/extra-bundle:3.8.0
      
    • 升级核心Twig匹配扩展包版本:
      composer require twig/twig:3.9.0 twig/intl-extra:3.9.0 twig/extra-bundle:3.9.0
      
  2. 验证扩展注册状态
    确保TwigExtraBundle已在Symfony的config/bundles.php中正确注册:

    return [
        // ...其他Bundle
        Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
    ];
    
  3. 清除项目缓存
    执行缓存清除命令,避免旧缓存残留导致的异常:

    php bin/console cache:clear
    

额外检查点

  • 确认user.createdAt是有效的DateTime/DateTimeImmutable对象,而非字符串或null,避免转换环节出现隐性错误。
  • 若使用Symfony Flex,不要手动修改Twig配置文件,确保扩展包自动加载逻辑未被破坏。

内容的提问来源于stack exchange,提问作者Cellophile

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 19:47:05