0
votes

I'm buiding an application on symfony2 and need a new twig filter :

I created a folder Twig in my bundle and created there this file AppExtension.php :

namespace App\MyBundle\Twig;

use Twig_Extension;
use Twig_Filter_Method;

class AppExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            'left' => new \Twig_Filter_Method($this, 'leftFilter'),
        );
    }

    public function leftFilter($string, $start = 0, $length = 1)
    {
        $left = substr($string, $start, $length);
        return $left;
    }

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

and then I declared it in services.yml :

app.twig.app_extension:
    class: App\MyBundle\Twig\AppExtension
    tags:
        - { name: twig.extension }

But I get this RuntimeException:

The autoloader expected class "App\MyBundle\Twig\AppExtension" to be defined in file ".../src/\App\MyBundle\Twig\AppExtension.php". The file was found but the class was not in it, the class name or namespace probably has a typo.

Can someone please tell me what I missed here ?

1
Are you sure there's no typo in your namespace (corrected while posting)? You can also try clearing symfony's cache and APC cache if you have.Touki
Hello and thanks for your help. I checked and rechecked before and now you say that I checked and rechecked again - this it as it is.G. Trennert
I cleared cache by deleting content of cache folders (command cache:clear shows same error) There must be an error but I can't see it !G. Trennert

1 Answers

1
votes

Use <php? instead <?.

That will solve the problem.