dpbz14739 2013-08-07 14:10
浏览 128
已采纳

登录Symfony2后运行控制台命令后台

I want to run a custom symfony2 console command background after login. I make a listener and try to use the process to run the command at background but the function not work well. Here is my code

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;
    }
}

Is there something wrong with my code? Thx for helping.

  • 写回答

1条回答 默认 最新

  • douxuanjie2692 2013-08-07 17:22
    关注

    Any variables that you need to access from inside your anonymous function you have to use a use statement. Further $this could conflict due to scope.

    $that = $this;
    $process->start(function ($type, $buffer) use ($that) {
        $command = $that->RecommendJobService;
        $input = new ArgvInput();
        $output = new ConsoleOutput();
        $command->execute($input, $output);
        echo "1";
    });
    

    Also you can take your anonymous function and test it outside of the start() method like this.

    $closure = function ($type, $buffer) use ($that) {
        $command = $that->RecommendJobService;
        $input = new ArgvInput();
        $output = new ConsoleOutput();
        $command->execute($input, $output);
        echo "1";
    };
    $closure();
    

    Then you can put some debugging in there and see if it runs. I'm not sure if echo is a good way to deal with the console. I would recommend Monolog or the $output->writeln($text); command.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛