0

I want to create HelperController for my project. I generate a controller with doctrine:generate:controller and I need to use entity manager in it.

I enjected to services.yml but it is giving an error like this:

Argument 1 passed to CampingBundle\Controller\HelperController::__construct() must be an instance of Doctrine\ORM\EntityManager, none given ...

My Controller Code :

namespace CampingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Doctrine\ORM\EntityManager;

class HelperController extends Controller
{
    protected $manager;

    public function __construct(EntityManager $manager)
    {
        $this->manager = $manager;
    }

My Services.yml :

services:
  camping.helper_controller:
      class: CampingBundle\Controller\HelperController
      arguments: ["@doctrine.orm.entity_manager"]

Why it doesn't work ? Shoudl I clear cache or something else or is there anything wrong in definition ?

Thanks

  • It does not work because of the way you have your route configured. You need a slight tweak to tell the controller resolver to pull the controller from the container instead of new'ing it. https://symfony.com/doc/current/controller/service.html#referencing-your-service-from-routing I am a bit suspicious of your controller name though. Hopefully your HelperController is not meant to help other controllers. – Cerad Feb 11 '18 at 15:30
  • If you have doctrine bundle installer, 1: you manager is accessible by calling $this->getDoctrine()... 2- as you extends base controller, you have access to $container, so you can register a service which handle your manager intialization and access it – timmz Nov 29 '18 at 12:21

2 Answers2

0

Try to use EntityManagerInterface and remove extends Controller. Check this link if you need CAS (Controllers as Services). Change protected $manager; to private $manager;

namespace CampingBundle\Controller;

use Doctrine\ORM\EntityManagerInterface;

class HelperController
{
    /**
     * @var EntityManagerInterface $entityManager
     */
    private $entityManager;

    /**
     * @param $entityManager
     */
    public function __construct(EntityManagerInterface $entityManager)
    {
        $this->entityManager = $entityManager;
    }
}
0TshEL_n1ck
  • 441
  • 3
  • 9
  • I tried this but it is still giving error like : Argument 1 passed to CampingBundle\Controller\HelperController::__construct() must implement interface Doctrine\ORM\EntityManagerInterface, none given, called in .. – Çağdaş DAĞ Feb 11 '18 at 13:51
  • Did you correctly implement DI to your CampingBundle? Add your `camping.helper_controller: class: CampingBundle\Controller\HelperController arguments: ["@doctrine.orm.entity_manager"]` to app/config/services.yml for check it. – 0TshEL_n1ck Feb 11 '18 at 13:54
  • And you need to find your service in service definitions. `bin/console debug:container | grep HelperController` – 0TshEL_n1ck Feb 11 '18 at 13:57
  • @0TshEL_n1ck - If he wants a regular controller then he will need to extend from the base controller class. And changing from EntityManager to EntityManagerInterface is not going to help when no arguments are being passed. One thing I found useful when answering questions was to reproduce the actual problem and then test any proposed solutions before posting. Just a thought. – Cerad Feb 11 '18 at 15:36
  • @Cerad, In my humble opinion, he needs to correctly implement Dependency injection for pass data. About the regular controller, I passed link about CAS. And I pointed out to him the basic things about EntityManagerInterface and access modifiers because without understanding these things, it will be hard for him. – 0TshEL_n1ck Feb 11 '18 at 16:34
  • @0TshEL_n1ck Well, my opinion is significantly humbler that yours. Humor. My point is that his original controller code is correct. He just needs to tweak his route definition. He will probably also discover that he will need to add setContainer to his service definition as well. Of course we are all guessing a little bit over the exact version of Symfony being used. Starting in 3.3 or so, autowire will make much of this moot. – Cerad Feb 11 '18 at 16:43
  • @Cerad I agree, we just guess what he needs :) But I think he doesn't need regular controller because he trying to configure service definition on the services.yml file. – 0TshEL_n1ck Feb 11 '18 at 17:04
0

I'll leave my two cents here as I had same issue and fixed it by adding tags to service.

something.validate.some_service:
  class: Path\To\Some\Validator
  arguments:
    - '@doctrine.orm.entity_manager'
  tags:
    - { name: validator.constraint_validator, alias: some_validator_alias }

How to Work with Service Tags by Symfony

Katka
  • 194
  • 1
  • 14