Symfony 2 Error: Call to a member function get() on a non-object

前端 未结 3 659
走了就别回头了
走了就别回头了 2021-01-14 09:44

I looked up some solutions, but none of the found fit my problem.

In one controller i create an instance of another controller

//Controller1
$mailCon         


        
相关标签:
3条回答
  • 2021-01-14 10:12

    faced this issue...but adding this in the controller service solved it for me

    calls:
        - [setContainer, ["@service_container"]]
    
    0 讨论(0)
  • 2021-01-14 10:14

    You can define your controller as service, then get it in another controller.

    In your services.yml define needed controller as a service:

    services:
        your_service_name:
            class: YourCompany\YourBundle\Controller\YourController
    

    Then in any controller you'll be able to get this service via container:

    $yourController = $this->get('your_service_name');
    

    There is some useful information about Controllers as Services in documentation

    Note from OP

    This answer is totally right, i just want to add something so this Answer works to 100% fine!

    After i changed the Controller to a Service the service container was missing, refering to this Question here.

    To set the container, and with that the fully funcionality of SF2, you need to add one line to the service.yml

    services:
        mail_controller:
            class: YourCompany\YourBundle\Controller\YourController
            calls:
                - [ setContainer, [ @service_container ]]
    
    0 讨论(0)
  • 2021-01-14 10:15
    $this->container->get('mailer');
    

    works?

    0 讨论(0)
提交回复
热议问题