Symfony2自定义Twig扩展 - 未找到类

时间:2015-02-24 10:57:54

标签: php symfony twig-extension

我在添加Twig扩展时遇到问题。我收到以下错误:

ClassNotFoundException in appDevDebugProjectContainer.php line 317:
Attempted to load class "AppExtension" from namespace "HouseBundle\Twig".
Did you forget a "use" statement for another namespace?

我的代码:

/Users/Sites/www/houses/src/housesBundle/Twig/AppExtension.php

<?php

namespace HouseBundle\Twig;

class AppExtension extends \Twig_Extension
{

    public function getFilters()
    {
        return array(new \Twig_SimpleFilter('price', array($this, 'priceFilter')),
    );


    public function priceFilter($number, $decimals = 0, $decPoint = '.', $thousandsSep = ',')
    {
        $price = number_format($number, $decimals, $decPoint, $thousandsSep);
        $price = '$'.$price;

        return $price;
    }

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

/Users/Sites/www/houses/app/config/services.yml

services:
    app.twig_extension:
        class: HouseBundle\Twig\AppExtension
        tags:
            - { name: twig.extension }

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

鉴于此文件路径:

/用户/站点/ WWW / anildave /房屋/ SRC /的 housesBundle /Twig/AppExtension.php

您使用了错误的命名空间

namespace HouseBundle\Twig;

应该是

namespace housesBundle\Twig 

并且

class: HouseBundle\Twig\AppExtension

应该是

class: housesBundle\Twig\AppExtension