在server:run之后写入控制台

时间:2017-02-07 22:06:48

标签: symfony

在Symfony中,我正在运行php bin / console server:run,它打印一些消息,如下所示:

jon@debian:~/Documents/SFLive-Paris2016-Workflow$ php bin/console server:run

 [OK] Server listening on http://127.0.0.1:8000                                 


 // Quit the server with CONTROL-C.                                             

PHP 7.0.15-1~dotdeb+8.1 Development Server started at Tue Feb  7 21:14:00 2017
Listening on http://127.0.0.1:8000
Document root is /home/jon/Documents/SFLive-Paris2016-Workflow/web
Press Ctrl-C to quit.
[Tue Feb  7 21:28:05 2017] 127.0.0.1:45237 [200]: /
[Tue Feb  7 21:28:05 2017] 127.0.0.1:45240 [200]: /favicon.ico
[Tue Feb  7 21:28:47 2017] 127.0.0.1:45244 [200]: /
[Tue Feb  7 21:28:47 2017] 127.0.0.1:45249 [200]: /_wdt/c2da7a

我有一个侦听用户切换的处理程序。它在services.xml中定义为:

    <service id="test.on_switch_user_success" class="AppBundle\EventListener\OnSwitchUserSuccessHandler">
        <argument type="service" id="twig" />
        <tag name="kernel.event_listener" event="security.switch_user" method="onSwitchUser" />
    </service>

为完整起见,OnSwitchUserSuccessHandler为:

<?php

namespace UserFlowBundle\EventListener;

use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Workflow\Registry;


use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventDispatcher;


class OnSwitchUserSuccessHandler {

    private $em;
    private $workflowRegstry;

    public function __construct(EntityManager $entityManager, Registry $workflowRegistry)
    {
        $this->em = $entityManager;
        $this->workflowRegistry = $workflowRegistry;
    }

    public function onSwitchUser(SwitchUserEvent $event) 
    {

        $repository = $this->em->getRepository('UserFlowBundle:LoginMessage');


        $dispatcher = new EventDispatcher();
        $dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {

            // get the output instance
            $output = $event->getOutput();
           // write something about the command
           $output->writeln(sprintf('This is a console message: <info>%s</info>', $message));
        });

    }

}

如何让onSwitchUser写入控制台输出?

1 个答案:

答案 0 :(得分:1)

我认为记录事件的最佳方法是使用Monolog并通过/var/logs/dev.log访问它或使用dump()VarDumper component)函数并使用配置文件跟踪所有转储。

如果您确实需要将消息打印到控制台,则可以利用 ConsoleCommandEvent 界面。

的appbundle /事件监听/ OnSwitchUserSuccessHandler.php

<?php

namespace AppBundle\EventListener;

use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Twig_Environment as Environment;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventDispatcher;

class OnSwitchUserSuccessHandler {

    // ...

    public function onSwitchUser(SwitchUserEvent $event) 
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {

         // get the output instance
         $output = $event->getOutput();

        // write something about the command
        $output->writeln(sprintf('This is a console message: <info>%s</info>', $message));
        }
    }

}


});