Search code examples
symfonysymfony4

Dependency on a non-existent service "templating"


I tried to use

# app/config/services.yml
services:
    project.controller.some:
        class: Project\SomeBundle\Controller\SomeController
        arguments: ['@templating']

and

namespace Project\SomeBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;

class SomeController
{
    private $templating;

    public function __construct(EngineInterface $templating)
    {
        $this->templating = $templating;
    }

    public function indexAction()
    {
        return $this->templating->render(
            'SomeBundle::template.html.twig',
            array(

            )
        );
    }
}

in Symfony 4 flex. Now I get the error

ServiceNotFoundException

The service "project.controller.some" has a dependency on a non-existent service "templating".

Please tell me how to solve this. My composer.json already contains "symfony/templating": "^4.0" but this seems not to be enough.


Solution

  • With Symfony 4 you can also use new DI features (already available since Symfony 3.3):

    They will simplify all to:

    # app/config/services.yml
    services:
        _defaults:
            autowired: true
    
        Project\SomeBundle\Controller\SomeController: ~
    

    If you want to know more with real before/after examples, read How to refactor to new Dependency Injection features in Symfony 3.3