phpsymfonytwiggravatartwig-extension

Twig Gravatar on Symfony 4


I'm a newbie on Symfony and I'm having a problem to integrate the 'ry167/twig-gravatar' package on my project.

First, I did :

$ composer require ry167/twig-gravatar 3.0.0

And after I modified my services.yaml, which looks like this :

services:
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
App\:
    resource: '../src/*'
    exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

App\Controller\:
    resource: '../src/Controller'
    tags: ['controller.service_arguments']

twig.extension.gravatar:
    class: \TwigGravatar
    arguments:
        $default: ~         e.g. 'monsterid'
        $size: ~            e.g. 50
        $filterPrefix: ~    e.g. 'foo'
        $rating: ~          e.g. 'x'
        $useHttps: true
    tags:
        - { name: twig.extension }

And finally, I have this on my view:

<p>{{ 'example@example.com'|grAvatar }}</p>

But I got this error:

Invalid service "twig.extension.gravatar": class "Twig_Extension" not found while loading "TwigGravatar"

Any ideas? I can't understand where my problem comes from...


Solution

  • You probably use Twig 3.* which removed all PSR-0 classes (with the underscore).

    The next version of ry167/twig-gravatar fixes the issue. There is already a release candidate.

    Option 1: Wait for next stable release

    If you want to wait for the stable release, then temporarily add a conflict block to your composer.json to use the latest Twig version before 3.0:

    {
        ...
        "require": {
           ...
           "ry167/twig-gravatar": "^3.0.0",
           ... 
        },
        "conflict": {
            "twig/twig": ">=3.0"
        }
    }
    

    Run composer update afterwards to let Composer do the work of figuring out the dependencies and downgrading your Twig version.

    You may remove the conflict when version 4.0 is released and you changed the dependency to ^4.0.0.

    Option 2: Use Release Candidate

    If you want to use the new version right away, you have to tell composer that non-stable versions are alright using stability flags.

    composer require ry167/twig-gravatar "^4.0.0@RC"