2015-10-27 85 views
0

我希望能够从URL和控制台运行控制器的方法。我怎样才能做到这一点?我的意思是,在某些控制器具有一个方法:从控制台运行路由方法

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

我希望能够通过http://domain.com/fooBarphp app/console fooBar,或像这样打开它。

控制台不工作。我该如何解决这个问题?

回答

2

你想要的是(我认为)在技术上是可行的,但不是好的做法。

您应该将控制器方法中的代码移到服务中,然后您可以从命令和控制器中运行相同的代码。

-1

你需要建立一个命令:

<?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'); 
} 

} 

更多信息,详情:https://reformatcode.com/code/c/wcf-sslstreamsecurity-dns-identity-check-failing-for-just-46-framework