user3862097
user3862097

Reputation: 21

twig extension with arguments

I am trying to see to make a twig extension but I hang on @security.context injection because it returns me an error for me to add the argument in the constructor? I'm several people searched the internet but the problem is I can not find the solution is what I do wrong?

The goal is to inject security.context to have access to the current user.

<?php
namespace Acme\AppBundle\Twig\Extension;

use Symfony\Component\Security\Core\SecurityContextInterface;

class reserveExtension extends \Twig_Extension
{
    protected $context;

    public function __construct(SecurityContext $context)
    {
        $this->context = $context;
    }

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

    function reserveCurrentUser($entity)
    {
        var_dump($this->context->getToken()->getUser());
    }

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

Service definition:

services:
reserveExtension:
    class: Acme\AppBundle\Twig\Extension\reserveExtension
    arguments: [@security.context]
    tags:
        - { name: twig.extension }

Error:

Catchable Fatal Error: Argument 1 passed to Acme\AppBundle\Twig\Extension\ReserveExtension::__construct() must be an instance of Acme\AppBundle\Twig\Extension\SecurityContext, instance of Symfony\Component\Security\Core\SecurityContext given, called in D:\wamp\www\Acme\app\cache\dev\appDevDebugProjectContainer.php on line 432 and defined

Thank you !

Upvotes: 0

Views: 559

Answers (1)

user2268997
user2268997

Reputation: 1391

Your use statement is wrong, you have use ...\SecurityContextInterface, but you have SecurityContext in your actual code, change one of them.

Upvotes: 1

Related Questions