【问题标题】:ClassNotFoundException: Attempted to load class "TwigExtension" from the global namespaceClassNotFoundException:试图从全局命名空间加载类“TwigExtension”
【发布时间】:2015-01-16 13:59:04
【问题描述】:

我正在尝试使用服务容器创建 Twig 扩展,但我得到了“ClassNotFoundException”。

这里例外:

ClassNotFoundException:试图从 全局命名空间在 \path\to\symfony-simple-blog\src\YagoQuinoy\SimpleBlogBu​​ndle\Twig\BlogExtension.php 第 11 行。您是否忘记了该类的 use 语句?也许你 需要为以下其中一项添加 use 语句: Symfony\Bundle\TwigBundle\DependencyInjection\TwigExtension

代码如下:

树枝扩展:

namespace YagoQuinoy\SimpleBlogBundle\Twig;

/**
 * Description of BlogExtension
 *
 * @author yagoquinoy@gmail.com
 */
class BlogExtension extends \TwigExtension
{

    public function getFilters() {
        return array(new \Twig_SimpleFilter('timeAgo', array($this, 'timeAgoFilter')));
    }

    public function timeAgoFilter() {

        return 'yolo!';
    }

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

service.yml

services:
    yago_quinoy_simple_blog.twig.blog_extension:
        class: YagoQuinoy\SimpleBlogBundle\Twig\BlogExtension
        tags:
            - { name: twig.extension }

编辑:已解决。

smarber Twig_Extension 不是 TwigExtension, symfony.com/doc/current/cookbook/template/twig_extension.html

【问题讨论】:

标签: php symfony dependency-injection twig twig-extension


【解决方案1】:

您仍然可以通过运行在 composer.json 中修复您的 twig 版本来更新到 Symfony 4.4

composer require twig/twig ~2.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    相关资源
    最近更新 更多