I have created a service at ./src/Service, and I want to use the Doctrine Entity Manager in my service, so I inject it in the __construct
method:
namespace App\Service;
use App\Entity\Category;
use Doctrine\ORM\EntityManagerInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class CommonPageGenerator
{
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var Environment
*/
private $templating;
public function __construct(EntityManagerInterface $em, Environment $templating)
{
$this->em = $em;
$this->templating = $templating;
}
public function page1($title){ return; }
}
I then inject this service in a controller:
/**
* @Route("/overseas", name="overseas")
* @param CommonPageGenerator $commonPageGenerator
*/
public function overseas(CommonPageGenerator $commonPageGenerator)
{
return $commonPageGenerator->page1('overseas');
}
But I get the following error:
Argument 1 passed to App\Service\CommonPageGenerator::__construct() must implement interface Doctrine\ORM\EntityManagerInterface, string given, called in /Users/tangmonk/Documents/mygit/putixin.com/putixin_backend/var/cache/dev/ContainerB7I3rzx/getCommonPageGeneratorService.php on line 11
My services.yaml
file:
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$em: 'doctrine.orm.default_entity_manager'
I am using Symfony 4.3
services.yaml
file unless you are on Sf4.3+ (not sure if it does exists since then) and you're usingautowiring
Check [this post ](stackoverflow.com/questions/10427282/…) it has a bunch of useful information that could help you – ReynierPM