我想知道我怎样才能跑交响乐2来自浏览器查询或控制器的命令。
这是因为我没有任何可能托管来运行它,并且每个 cron 作业都是由管理员设置的。
我什至没有启用exec()
因此,当我想测试它时,我必须将命令中的所有内容复制到某个测试控制器,这不是最佳解决方案。
See 官方文档 https://symfony.com/doc/current/console/command_in_controller.html对于较新版本的 Symfony,此问题
您不需要从控制器执行命令的服务,我认为最好通过run
方法而不是通过控制台字符串输入,但是官方文档 http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command建议您通过它的别名来调用命令。另请参阅这个答案 https://stackoverflow.com/a/11755640/541961。在 Symfony 2.1-2.6 上测试。
您的命令类必须扩展ContainerAwareCommand
// Your command
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
class MyCommand extends ContainerAwareCommand {
// …
}
// Your controller
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
class SomeController extends Controller {
// …
public function myAction()
{
$command = new MyCommand();
$command->setContainer($this->container);
$input = new ArrayInput(array('some-param' => 10, '--some-option' => true));
$output = new NullOutput();
$resultCode = $command->run($input, $output);
}
}
大多数情况下你不需要BufferedOutput
(from Jbm's答案)并且检查一下就足够了$resultCode is 0
,否则就会出现错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)