Search code examples
phpsymfonycontrollersymfony-process

Call controller/action in Console Commend


I want to import data from LinkedIn and save them in my database. I want this run at background.

So I create a console command. But how could I call a controller/action in a console command so that the import and save transaction could run at the console command at background?


Solution

  • I don't know what the structure of your application is, but if you want to allow a command to run something that a controller does, then the typical way to do that is by having the intended code in a service that both controller and command has access to.

    By using a ContainerAwareCommand, you give the command instance access to the service container, and thusly the service. Controllers by default have access to the service container.

    Try to avoid jury rigging parts of your application together when they are designed to be separate. Give them access to the same services, but keep them apart.