Php 登录Symfony2后运行控制台命令后台
我想在登录后在后台运行自定义symfony2控制台命令。我制作了一个监听器,并尝试使用该进程在后台运行该命令,但该函数无法正常工作。 这是我的密码Php 登录Symfony2后运行控制台命令后台,php,symfony,process,controller,listener,Php,Symfony,Process,Controller,Listener,我想在登录后在后台运行自定义symfony2控制台命令。我制作了一个监听器,并尝试使用该进程在后台运行该命令,但该函数无法正常工作。 这是我的密码 class LoginListener { protected $doctrine; private $RecommendJobService; public function __construct(Doctrine $doctrine) { $this->doctrine = $doctrin
class LoginListener
{
protected $doctrine;
private $RecommendJobService;
public function __construct(Doctrine $doctrine)
{
$this->doctrine = $doctrine;
}
public function onLogin(InteractiveLoginEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
if($user)
{
$process = new Process('ls -lsa');
$process->start(function ($type, $buffer) {
$command = $this->RecommendJobService;
$input = new ArgvInput();
$output = new ConsoleOutput();
$command->execute($input, $output);
echo "1";
});
}
}
public function setRecommendJobService($RecommendJobService) {
$this->RecommendJobService = $RecommendJobService;
}
}
我的代码有问题吗?谢谢您的帮助。您需要从匿名函数内部访问的任何变量都必须使用
use
语句。此外,由于范围的原因,这可能会发生冲突
$that = $this;
$process->start(function ($type, $buffer) use ($that) {
$command = $that->RecommendJobService;
$input = new ArgvInput();
$output = new ConsoleOutput();
$command->execute($input, $output);
echo "1";
});
您还可以使用匿名函数在start()方法之外进行测试,如下所示
$closure = function ($type, $buffer) use ($that) {
$command = $that->RecommendJobService;
$input = new ArgvInput();
$output = new ConsoleOutput();
$command->execute($input, $output);
echo "1";
};
$closure();
然后你可以在那里进行一些调试,看看它是否运行。我不确定echo是否是处理控制台的好方法。我建议使用独白或
$output->writeln($text)代码>命令。您所说的“功能不正常”是什么意思?有什么事吗?错误?什么都没发生。start()函数没有任何作用。谢谢您的回答。函数中的代码可以工作。我认为问题在于流程->开始不起作用。因为我在函数中放了一个记录器,所以它不会显示在日志中。当我更改start with run()函数时,一切正常。但我必须等待过程完成,而不是让它在后台工作。Thx@FlipI检查了start()
方法,它应该接受回调。但是手册将回调放在run()
方法中。试试这个:谢谢。这对我帮助很大。