user9378064
user9378064

Reputation:

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.

Upvotes: 2

Views: 4670

Answers (3)

yvoyer
yvoyer

Reputation: 7516

Another solution is to add the configuration under framework as explained in the doc

# app/config/packages/framework.yaml
framework:
    # ...
    templating: { engines: ['twig'] }

Upvotes: 2

Tomas Votruba
Tomas Votruba

Reputation: 24280

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

Upvotes: 1

Jakub Krawczyk
Jakub Krawczyk

Reputation: 970

Symfony 4 doesn't include Twig by default, so you need to install it first:

composer require twig

should do the trick. Also, with service autowiring in Symfony 4 you don't need to manually declare it in the services.yml.

Upvotes: 3

Related Questions