【问题标题】:Twig extension seems to cause attempt to load non existent cache fileTwig 扩展似乎导致尝试加载不存在的缓存文件
【发布时间】:2016-08-23 10:52:23
【问题描述】:

我有这个基于 silex 的网站的树枝扩展:

<?php
namespace UT\Provider;

/**
 * Twig extension for containing any additional twig functions we need
 */
class TwigUTProvider extends \Twig_Extension {

    public function getName() {
        return 'ut_functions';
    }

    public function getFunctions() {
        return [
            new \Twig_SimpleFunction("maybeDecodeJapanse", [$this, \UT\Provider\TwigUTProvider::maybeDecodeJapanse()]),
        ];
    }

    public function maybeDecodeJapanse() {
        return 'a string';
    }
}

在应用程序文件中注册如下:

$this->register(new Provider\TwigServiceProvider, [
            'twig.path' => [
                __DIR__ . '/Resources/Templates',
                $this['docroot'] . '/silex/vendor/braincrafted/bootstrap-bundle/Braincrafted/Bundle/BootstrapBundle/Resources/views/Form',
                __DIR__ . '/../Floso/Templates',
            ],
            'twig.options' => [
                'auto_reload' => true,
                'cache' => $this['debug_mode'] ? false : ($this['docroot'] . '/silex/var/cache/twig'),
                'debug' => $this['debug_mode'],
            ],
        ]);

        # Add Twig extensions
        $this['twig'] = $this->share($this->extend('twig', function ($twig) {
            $twig->addExtension(new \UT\Provider\TwigUTProvider());
            $twig->addExtension(new BootstrapBundle\Twig\BootstrapIconExtension('glyphicon'));
            $twig->addExtension(new BootstrapBundle\Twig\BootstrapLabelExtension);
            $twig->addExtension(new BootstrapBundle\Twig\BootstrapBadgeExtension);
            $twig->addExtension(new BootstrapBundle\Twig\BootstrapFormExtension);
            $twig->addExtension(new \Twig_Extension_StringLoader());

            $twig->getExtension('core')->setTimezone('Europe/London');

            return $twig;
        }));

这似乎是正确的 - 在扩展类名称中引入拼写错误会导致与我所坚持的不同的异常。

我在我的树枝模板中使用{{ maybeDecodeJapanese() }} 调用了这个扩展,这导致了这个异常(它似乎不是由模板调用本身引起的,拼错它会生成一个标准函数未找到异常):

Class '__TwigTemplate_3318c38dfd9c3eb0c4193184a517ff94fdd975ffb45d7f0a8f7490718f3bc1ef' not found

这发生在 /silex/vendor/twig/twig/lib/Twig/Environment.php 中

我最好的猜测是这是某种缓存文件。缓存在开发环境中被禁用,但我已经尝试删除缓存文件夹内容,但没有帮助。谷歌搜索尚未提供任何其他线索。

找到问题根源的任何帮助都会非常有帮助。

【问题讨论】:

  • 请注意,您可能在Japanse 中忘记了e

标签: php twig silex twig-extension


【解决方案1】:

我认为函数定义有错误。尝试将其更改为:

...new \Twig_SimpleFunction("maybeDecodeJapanse", [$this, "maybeDecodeJapanse"]),...

【讨论】:

  • 它有效!这让我整个下午都发疯了。谢谢!
猜你喜欢
  • 2011-06-17
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多