如何在Symfony 3中解决Twig_Error_Runtime

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Symfony 3中解决Twig_Error_Runtime相关的知识,希望对你有一定的参考价值。

我继承了一个相当复杂的Symfony 3项目(我现在主要使用Laravel - 自从我使用Symfony以来已经有好几年了)所以这有点超出了我的驾驶室。

我做了一个作曲家更新,它构建好了。

composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 0 installs, 1 update, 0 removals
  - Updating twig/twig 1.x-dev (e8555dc => 1445246):  Checking out 1445246d8e
Writing lock file
Generating autoload files
> IncenteevParameterHandlerScriptHandler::buildParameters
Updating the "app/config/parameters.yml" file
> SensioBundleDistributionBundleComposerScriptHandler::buildBootstrap
> SensioBundleDistributionBundleComposerScriptHandler::clearCache

 // Clearing the cache for the dev environment with debug
 // true


 [OK] Cache for the "dev" environment (debug=true) was successfully cleared.


> SensioBundleDistributionBundleComposerScriptHandler::installAssets

 Trying to install assets as relative symbolic links.

 -- -------- ----------------
     Bundle   Method / Error
 -- -------- ----------------


 [OK] All assets were successfully installed.


> SensioBundleDistributionBundleComposerScriptHandler::installRequirementsFile
> SensioBundleDistributionBundleComposerScriptHandler::prepareDeploymentTarget

我清除了缓存

php bin/console cache:clear --no-warmup --env local

当我浏览到我的本地开发URL时,我得到:

Whoops, looks like something went wrong.

(4/4) Twig_Error_Runtime
An exception has been thrown during the rendering of a template ("Notice: Undefined offset: 0").

in exception_full.html.twig (line 1)
at Twig_Template->displayWithErrorHandling(array('status_code' => 500, 'status_text' => 'Internal Server Error', 'exception' => object(FlattenException), 'logger' => object(Logger), 'currentContent' => '', 'app' => object(AppVariable)), array('head' => array(object(__TwigTemplate_ed2c7b0206a4a8ba0c01ceb7e7a7f0ba342d3cbf55c912fe1f34e6abd35411c0), 'block_head'), 'title' => array(object(__TwigTemplate_ed2c7b0206a4a8ba0c01ceb7e7a7f0ba342d3cbf55c912fe1f34e6abd35411c0), 'block_title'), 'body' => array(object(__TwigTemplate_ed2c7b0206a4a8ba0c01ceb7e7a7f0ba342d3cbf55c912fe1f34e6abd35411c0), 'block_body')))
in Template.php (line 403)

<snip/>

(3/4) ContextErrorException
Notice: Undefined offset: 0

in Profiler.php (line 23)
at Twig_Extension_Profiler->enter(object(Twig_Profiler_Profile))
in ProfilerExtension.php (line 40)

<snip/>

(2/4) Twig_Error_Runtime
An exception has been thrown during the rendering of a template ("Notice: Undefined offset: 0").

in base.html.twig (line 11)
at Twig_Template->displayBlock('javascripts', array('env' => 'local', 'app' => object(AppVariable)), array('title' => array(object(__TwigTemplate_e199b3b21ccad34239564ea288dce2bfa2f11212622c2c75863848c603e82b70), 'block_title'), 'stylesheets' => array(object(__TwigTemplate_e199b3b21ccad34239564ea288dce2bfa2f11212622c2c75863848c603e82b70), 'block_stylesheets'), 'body' => array(object(__TwigTemplate_888836406c51f4232014f646f747310c541800b61ef418c11fbfcee42a1afe9b), 'block_body'), 'javascripts' => array(object(__TwigTemplate_e199b3b21ccad34239564ea288dce2bfa2f11212622c2c75863848c603e82b70), 'block_javascripts')))
in Environment.php(467) : eval()'d code (line 51)

<snip/>

(1/4) ContextErrorException
Notice: Undefined offset: 0

in Profiler.php (line 23)
at Twig_Extension_Profiler->enter(object(Twig_Profiler_Profile))
in ProfilerExtension.php (line 40)

由于这似乎是供应商文件的问题,我不知道从哪里开始。

日志中的最后一个错误是

[2018-03-02 15:56:53] request.CRITICAL: Exception thrown when handling an exception (Twig_Error_Runtime: An exception has been thrown during the rendering of a template ("Notice: Undefined offset: 0"). at /Projects/myproject/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception_full.html.twig line 1) {"exception":"[object] (Twig_Error_Runtime(code: 0): An exception has been thrown during the rendering of a template ("Notice: Undefined offset: 0"). at /Projects/myproject/vendor/symfony/symfony/src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception_full.html.twig:1, Symfony\Component\Debug\Exception\ContextErrorException(code: 0): Notice: Undefined offset: 0 at /Projects/myproject/vendor/twig/twig/lib/Twig/Extension/Profiler.php:23)"} []
答案

尝试在composer.json“twig / twig”中添加这一行:“2.4.4”

对我来说工作。似乎twig 2.4.5的最后更新有这个问题。

我在这里找到了答案:https://github.com/twigphp/Twig/issues/2627

以上是关于如何在Symfony 3中解决Twig_Error_Runtime的主要内容,如果未能解决你的问题,请参考以下文章

如何在Symfony 3中覆盖FOSUserBundle的ProfileController?

如何自动将用户对象传递给 Symfony 3 表单?

如何使用 Composer 和 Symfony 解决错误 255 [关闭]

如何解决在 aws 上打开 symfony2 安装时出现 403 Forbidden 错误?

Hwi oauth bundle 和 Symfony 3.4 无法自动装配服务:如何在 symfony 3.4 + FOSUserBundle 中使用 hwi/oauth-bundle

如何在 Symfony 3 中正确存储日期时间?