FatalErrorException: Error: Call to a member function has() on a non-object

后端 未结 3 1714
感情败类
感情败类 2020-12-16 05:01

I have a read a lot of topics on this and I can\'t seem to find a solution to my problem.

I feel like the problem is obvious and maybe I have just been staring at

3条回答
  •  -上瘾入骨i
    2020-12-16 05:23

    Not sure why you would make a controller a service. For what use case? Normally a service is a Plain Old PHP Object.

    About your problem .. since you are using the controller as a service it does not get the container automatically. So you have to inject the entire container, which is kind of heavy if you just need doctrine.

    So it's better just to inject the things you really need. To inject doctrine, in your yml below class:

    arguments: ["@doctrine.orm.entity_manager"]
    

    Then in your controller constructor:

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

    Possible you will need to call the parent constructor (be aware of that).

    If you want do inject the complete service container anyway, here is the right section in the manual how you can do that: http://symfony.com/doc/current/cookbook/service_container/scopes.html#passing-the-container-as-a-dependency-of-your-service

提交回复
热议问题