从控制台运行路由方法

I want to be able to run a controller's method from URL, and from the console. How can I do that? I mean, having a method in some controller:

/**
 * @Route("/fooBar", name="fooBar")
 */
public function actionFooBar() {
    $this -> get('file') -> saveSomethingToSomeFile();
    return 'a';
}

I want to be able to open it via http://domain.com/fooBar and php app/console fooBar, or something like this.

The console one doesn't work. How can I solve this?

What you want is (I think) technically doable, but not good practice.

You should move the code in your controller method to a service, then you can run that same code from both your command and your controller.

You need to build a Command:

<?php

namespace Application\CommandBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManager;

/**
 * Class testCommand
 * @package Application\CommandBundle\Command
 */
class TestCommand extends ContainerAwareCommand
{

/**
 * Configuration of the command
 */
protected function configure()
{
    $this
        ->setName('command:do:something')
        ->setDescription('This command does something');
}

protected function initialize(InputInterface $input, OutputInterface $output)
{

}

/**
 * @param InputInterface  $input  An InputInterface instance
 * @param OutputInterface $output An OutputInterface instance
 *
 * @return null|int null or 0 if everything went fine, or an error code
 */
protected function execute(InputInterface $inputInterface, OutputInterface $outputInterface)
{
    $outputInterface->writeln(
        'This command does something <info>' . $inputInterface->getOption('env') . '</info> environment'
    );

    $this->getContainer()->get('application_command.test')->doSomething();

    $outputInterface->writeln('Done');
}

}

More info with details: https://reformatcode.com/code/c/wcf-sslstreamsecurity-dns-identity-check-failing-for-just-46-framework