J.L
J.L

Reputation: 592

How can I run a function in a controller using the command line

Currently, I created a command, and I'm trying to use the command to call one function in the controller.

So I think the first step is to make the controller as a service that the command line can call.

According to the Symfony2 online book: in the services.yml:

parameters:
   property.controller.core.class: Ladoo\Brolly\CoreBundle\Controller\PropertyController

services: 
  property.core.controller:
        class: '%property.controller.core.class'

And in the command php file:

protected function execute(InputInterface $input, OutputInterface $output)
{
    $this->forward('property.core.controller:updatePropertyAction');
}

But the result says that forward is not defined.

And my question is how to fix that problem, and how to run a function in controller using the command line. Let me know if my steps are wrong.

Upvotes: 0

Views: 150

Answers (1)

drymek
drymek

Reputation: 335

If you have a updatePropertyAction method in the controller, you should be able to:

$this->getContainer()->get('property.core.controller')->updatePropertyAction("arguments");

To access to container form command you should make it ContainerAware:

class GreetCommand extends ContainerAwareCommand // see: http://symfony.com/doc/current/cookbook/console/console_command.html

Personally, I would not use controllers in command (a bad practice). I would consider writing a Manager service. It would do exactly what you need and use it in both, Controller and Command.

Upvotes: 2

Related Questions