Search code examples
phpsymfonytwig-extension

Symfony2 custom Twig Extension - Class not found


I'm having an issue adding in a Twig extension. I'm getting the following error:

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

My code:

/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 }

Any help would be much appreciated.


Solution

  • Given this filepath:

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

    You are using the wrong namespace

    namespace HouseBundle\Twig;
    

    Should be

    namespace housesBundle\Twig 
    

    And

    class: HouseBundle\Twig\AppExtension
    

    Should be

    class: housesBundle\Twig\AppExtension