symfonysymfony-process

Controller Action From Process


Symfony 2.8

Being in a Service called from a Command, Is there any way to execute/invoke a Controller Action as a Process Object? Or do I have to covert that Controller Action into a Command in order to be called by a Process?

I need to execute iterative BL (business logic) elements, already coded into Controller Actions.

The invoking chain is:

Scheduled Command --> Service Container --> N Processes or whatever with BL.

Service must control/monitor the Processes execution. (start, run, stop, etc..)

Any other way?

Cheers!!


Solution

  • In order to avoid duplicating the business logic from the controller action, create a new service and declare it into the service container, you specified you are using symfony 2.8 so there's no autowire feature available and you need to declare it manually

    services:
        app.custom_service:
            class:     AppBundle\Service\BusinessLogicService
            arguments: []
    

    Then you can use it in your controller injecting it via (dependency injection) or simply call it from the container service

    // the container will instantiate a new BusinessLogicService()
    $service = $this->container->get('app.custom_service');
    

    For the command, you can implement the ContainerAwareInterface and again call your service from the service container

    ex:

    class BusinessLogicCommand extends Command implements ContainerAwareInterface
    {
    
        public function getBusinessLogicService()
        {
            return $this->getContainer()->get('app.custom_service');
        }
    }