Search code examples
symfonysymfony-2.3

How to call controller function from other controller In symfony 3?


I am new in symfony and I want to call base controller function from other controller. main purpose behind is there is Some common process(code) for all controller so I made one common function in base controller so I able to access function from every controller in sonata admin controller as well as other normal controller but I have not any idea about this, can any one know about this then suggest me. Thanks in advance


Solution

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

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

    services:
        service_name:
            class: BundleName\Controller\YourControllerName
    

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

    $otherController = $this->get('service_name');
    $otherController->methodName();