首页 > 解决方案 > Symfony 缓冲区内容到树枝模板

问题描述

我有一个运行长服务的控制器,它在 ob_implicit_flush 和 ob_end_flush 中输出不同的消息......

我的问题是如何将此内容刷新到树枝模板...

这是控制器:

<?php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Sync\Incomming\Syncronize;


/**
* @Route("/sync")
*/
class SyncController extends AbstractController
{
    private $syncronize;
    public function __construct(Syncronize $syncronize)
    {
        $this->syncronize = $syncronize;

    }
    /**
     * @Route("/",name="sync_index")
     */
    public function index(Request $request)
    {  


        ini_set('memory_limit', '-1');
        ini_set('max_execution_time', 300);
        header('X-Accel-Buffering: no');
        ob_implicit_flush(true);
        $this->syncronize->updateData();
        ob_end_flush(); 
        /*How can i pass this content to the twig template ?*/
        return $this->render( 'sync/output.html.twig', ['message' => "" ]); 

    }

}

这是发送消息的工具:

namespace App\Tools;

class Message
{
    static function write( string $msg, $level = 1 )
    {
        $levels=[
            '-',
            '  |_',
            '    |_',
            '      |_',
            '        |_',
        ];
        ob_implicit_flush(true);
        echo $levels[$level] . $msg . PHP_EOL;
        return ob_flush();
    }

}

有任何想法吗 ?谢谢...

标签: symfonytwigbuffer

解决方案


在您的索引操作中,ob_end_flush()您可以调用而不是调用$messages =ob_get_flush().

这也将停止输出缓冲,但它会将缓冲区的内容作为字符串返回,然后您可以在 twig 模板中使用它。


推荐阅读