/ / Dependência de um serviço inexistente “template” - symfony, symfony4

Dependência de um serviço inexistente “template” - symfony, symfony4

Eu tentei usar

# app/config/services.yml
services:
project.controller.some:
class: ProjectSomeBundleControllerSomeController
arguments: ["@templating"]

e

namespace ProjectSomeBundleController;

use SymfonyBundleFrameworkBundleTemplatingEngineInterface;
use SymfonyComponentHttpFoundationResponse;

class SomeController
{
private $templating;

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

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

)
);
}
}

no Symfony 4 flex. Agora recebo o erro

ServiceNotFoundException

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

Por favor, me diga como resolver isso. Meu composer.json já contém "symfony / templating": "^ 4.0", mas isso parece não ser suficiente.

Respostas:

0 para resposta № 1

Com Symfony 4 Você também pode usar novos recursos DI (já disponíveis desde o Symfony 3.3):

Eles simplificam tudo para:

# app/config/services.yml
services:
_defaults:
autowired: true

ProjectSomeBundleControllerSomeController: ~

Se você quiser saber mais com exemplos reais antes / depois, leia Como Refatorar os Novos Recursos de Injeção de Dependência no Symfony 3.3


1 para resposta № 2

O Symfony 4 não inclui o Twig por padrão, então você precisa instalá-lo primeiro:

composer require twig

deve fazer o truque. Além disso, com a autowiring de serviço no Symfony 4 você não precisa declará-lo manualmente no services.yml.