Symfony 2.8 injecting twig Symfony 2.8 injecting twig symfony symfony

Symfony 2.8 injecting twig


try to change twig with EngineInterface like this:

services:    appbundle.newsletter:        class: AppBundle\Model\Newsletter        arguments: ['@templating']

And

namespace AppBundle\Model;use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;class Newsletter{    private $twig;    public function __construct(EngineInterface $templating)    {        $this->twig = $templating;    }}

After you need o call the service:

$this->get('service_name');


When you call $newsletter = new Newsletter(); you're not accessing the service through the dependency injection container, ignoring the service definition you did.

To leverage the DIC and have your dependencies like @twig injected on your service, you should grab the service instance using the service container. To do that on a Symfony controller you can do it like this:

$newsletter = $this->get('appbundle.newsletter');

@twig is gonna be injected in your service as defined on the yaml file and it is shared between every place you use your service again through the container.