php – Symfony在发送响应后运行代码

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Symfony在发送响应后运行代码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我看了 this other question.我正在寻找一种方法来做那个问题的OP也想要的,那就是 continue processing php after sending http response,但是在Symfony2中.

我实现了一个在每次内核终止后触发的事件.到目前为止一切都那么好,但我想要的是它在CERTaiN终止后,在特定的控制器动作中触发,例如在发送表单之后,而不是每次请求时.那是因为我想在某些时候做一些繁重的任务,并且不希望最终用户等待页面加载.

知道怎么能这样做?

<?PHP


namespace MedAppBundle\Event;

use JMS\diextraBundle\Annotation\InjectParams;
use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Tag;
use Psr\LOG\LoggerInterface;
use Symfony\component\DePEndencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use JMS\DiExtraBundle\Annotation\Inject;
/**
 * Class MedicListener
 * @package MedAppBundle\EventListener
 * @Service("medapp_test.listener")
 * @Tag(name="kernel.event_subscriber")
 */
class TestListener implements EventSubscriberInterface
{
    PRivate $container;

    private $logger;

    /**
     * Constructor.
     *
     * @param ContainerInterface $container A ContainerInterface instance
     * @param LoggerInterface $logger A LoggerInterface instance
     * @InjectParams({
     *     "container" = @Inject("service_container"),*     "logger" = @Inject("logger")
     * })
     */
    public function __construct(ContainerInterface $container,LoggerInterface $logger = null)
    {
        $this->container = $container;
        $this->logger = $logger;
    }

    public function onTerminate()
    {
      $this->logger->notice('fired');
    }

    public static function getSubscribedEvents()
    {
        $listeners = array(KernelEvents::TERMINATE => 'onTerminate');

        if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
            $listeners[ConsoleEvents::TERMINATE] = 'onTerminate';
        }

        return $listeners;
    }
}

到目前为止,我已经将事件订阅到kernel.terminate一个,但显然这会在每次请求时触发它.我把它变成了Swiftmailer的EmailSenderListener

内核必须每次都听这个事件,即使它没有被触发也感觉有点奇怪.我宁愿只在需要时解雇它,但不知道该怎么做.

在onTerminate回调中,您将获得 PostResponseEvent的实例作为第一个参数.您可以从该对象获取请求以及响应.
然后,您应该能够决定是否要运行实际的终止代码.

您还可以将自定义数据存储在请求的属性包中.看到这个链接Symfony and HTTP Fundamentals

您的代码可能如下所示:

// ...

class TestListener implements EventSubscriberInterface
{
    // ...

    public function onTerminate(PostResponseEvent $event)
    {
        $request = $event->getRequest();
        if ($request->attributes->get('_route') == 'some_route_name') {
            // do stuff
        }
    }

    // ...
}

编辑:

kernel.terminate事件旨在在发送响应后运行.但是symfony文档说的如下(摘自here):

编辑2:

要使用here中的解决方案,您可以直接编辑web / app.PHP文件以将其添加到那里(但这是某种“黑客核心”imo,即使它比以下更容易使用).或者你可以这样做:

>向具有高优先级的kernel.request事件添加侦听器并启动输出缓冲(ob_start).
>向kernel.response添加一个侦听器,并将标头值添加到响应中.
>将另一个具有最高优先级的侦听器添加到kernel.terminate并执行刷新(ob_flush,flush).
>在kernel.terminate的优先级较低的单独侦听器中运行代码

我没试过,但它确实应该有效.

脚本宝典总结

以上是脚本宝典为你收集整理的php – Symfony在发送响应后运行代码全部内容,希望文章能够帮你解决php – Symfony在发送响应后运行代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。