-1

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

Sandeep Gajera
  • 229
  • 4
  • 14
  • 1
    Just paste your title into the search bar and start reading. Wanting to call a controller from another controller is a common beginner's mistake. You seldom need to do so. Besides reading the answers, take a look at the service container portion of the manual as well as the best practices section. – Cerad Nov 15 '17 at 12:54

2 Answers2

2

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();
Mert Simsek
  • 1,550
  • 9
  • 19
  • Thanks For help, it working well in normal controller but In sontata admin controller i put below code then it give me error. **********************Code*********************** $BaseController = $this->get('common_fetch_department_details'); return $BaseController->testingAction(); *********************End Code************************ it give me Error: Attempted to call an undefined method named "get" of class "AppBundle\Admin\MedicalProviderAdmin". Please suggest me how to call same service function in sonata admin – Sandeep Gajera Nov 15 '17 at 12:29
  • How I can call normal controller function from sonata controller , please suggest me? – Sandeep Gajera Nov 15 '17 at 12:36
  • @SandeepGajera please incorporate it to your question or ask another one. – svgrafov Nov 15 '17 at 12:37
  • 4
    You should never (AFAIK) call a controller function from another controller. It looks like your controller code should be shared, and this is what services are for. So create one and use it as Mert suggests. – zed Nov 15 '17 at 13:18
0

Since Symfony services are now private by default, it's cleaner to inject just the controller you want.


    class AppController extends AbstractController
{
    private ImportService $importService;
    private ParameterBagInterface $bag;
    private BillRepository $billRepository;

    public function __construct(ImportService $importService, ParameterBagInterface $bag, BillRepository $billRepository)
    {
        $this->importService = $importService;
        $this->bag = $bag;
        $this->billRepository = $billRepository;
    }

    /**
     * @Route("/app_load", name="app_load")
     */
    public function load()
    {
//        import, or whatever.

Now inject the controller. For example, in the doctrine fixtures loader.

namespace App\DataFixtures;

use App\Controller\AppController;
use App\Services\ImportService;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Psr\Log\LoggerInterface;

class AppFixtures extends Fixture
{
    private AppController $appController;
    private LoggerInterface $logger;

    public function __construct(AppController $appController, LoggerInterface $logger)
    {
        $this->appController = $appController;
        $this->logger = $logger;
    }
    public function load(ObjectManager $manager)
    {
        $this->appController->load();
        $this->logger->info("Loaded.");
    }
}
Tac Tacelosky
  • 3,165
  • 3
  • 27
  • 28