Call controller/action in Console Commend

361 Views Asked by At

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?

2

There are 2 best solutions below

8
On BEST ANSWER

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.

0
On

Thanks to Flosculus and sensi for helping me a lot. In order to reach my goal, I first add a event listener when login.http://dev.dbl-a.com/symfony-2-0/how-to-add-a-symfony2-login-event-listener/ Then I add a process to make it work in background http://symfony.com/doc/master/components/process.html