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
faced this issue...but adding this in the controller service solved it for me
calls:
- [setContainer, ["@service_container"]]
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 ]]
$this->container->get('mailer');
works?