如何调试symfony 6中的“如果Twig包不可用,则无法使用“renderView”方法”错误?

yyhrrdl8  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(89)

我最近刚把我的项目从symfony 4升级到6,没有太多问题。目前的在线版本使用symfony 6没有问题。
我现在正试图清理代码,删除一些无用的部分和其他小修改,我刚刚遇到了一个奇怪的问题。我的所有页面现在都给我错误:
如果Twig Bundle不可用,则不能使用“renderView”方法。尝试运行“composer require symfony/twig-bundle”。
问题是:软件包已经存在,所以推荐的命令什么也不做。
我尝试检查触发异常的代码:

protected function renderView(string $view, array $parameters = []): string
    {
        if (!$this->container->has('twig')) {
            throw new \LogicException('You cannot use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
        }

    ...
    }

字符串
因此,我使用bin/console debug:container twig检查了twig服务,它提供了与当前工作版本相同的输出。
有一个注解:
![注意]编译容器时,“twig”服务或别名已被删除或内联。
但是这个注解也存在于我的生产代码中,没有问题。
我没有对两个版本之间的控制器文件进行真实的的修改,主要的区别是文件移动和名称空间适应这种变化。
我没有任何想法,除了重新做所有这些改变,但我担心我会落在同样的结果。
编辑:
问题看起来比我想象的要大,因为我在安全捆绑包中遇到了同样的问题。
我尝试在控制器方法中注入\Twig\Environment,它自动连接没有问题。
我试着用composer recipes更新所有的食谱,没有有趣的结果。
我试着用twig将它与新创建的Symfony 6控制器进行比较,但没有发现明显的差异,除了使用php属性,我试图在我的项目中使用,也没有结果。
bin/console debug:container HomeController的结果:

Option             Value 
----------------- -------------------------------
Service ID         App\Controller\HomeController 
Class              App\Controller\HomeController 
Tags               controller.service_arguments 
                   container.service_subscriber 
Calls              setContainer 
Public             yes 
Synthetic          no 
Lazy               no 
Shared             yes 
Abstract           no 
Autowired          yes 
Autoconfigured     yes
Usages             none


编辑:
老实说,如果twig现在是一个私有服务,不能通过容器访问,为什么AbstractController仍然使用这行:

if (!$this->container->has('twig')) {
            throw new \LogicException('You cannot use the "renderView" method if the Twig Bundle is not available. Try running "composer require symfony/twig-bundle".');
        }


那不是会给大家造成错误吗?那么为什么不呢,而且只是现在对我来说,而不是对我目前的版本(我的生产是与相同的Symfony 6软件包操作)
编辑:
通过在供应商中运行,我设法发现在我的原始代码中,ServiceLocator用于AbstractController,但在我的bug中,它似乎是使用的缓存Container类(作为$this->container)。
所以调用的has()方法不一样…

shyt4zoc

shyt4zoc1#

有点晚了,但我遇到了类似的问题,并找到了解决方案,在我的“控制器即服务”定义中注入了Psr\Container\ContainerInterface而不是service_container

My\CustomBundle\Controller\:
    resource: '../../Controller/'
    tags: ['controller.service_arguments', 'container.service_subscriber']
    public: true
    calls:
        -   method: setContainer
            arguments: ['@Psr\Container\ContainerInterface']

字符串
感谢这个github讨论找到了解决方案:https://github.com/symfony/symfony/discussions/44628

相关问题