0

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.