symfony - 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();
}
}
有任何想法吗 ?谢谢...
解决方案
在您的索引操作中,ob_end_flush()
您可以调用而不是调用$messages =
ob_get_flush()
.
这也将停止输出缓冲,但它会将缓冲区的内容作为字符串返回,然后您可以在 twig 模板中使用它。
推荐阅读
- javascript - 我怎样才能让我的标题不与我在 fullpagejs 中的幻灯片一起移动
- linux - Linux 共享库版本匹配
- python - 每次函数运行时向字符串添加单个字符并返回一次长度为 4,Python
- html - 无法导出 Android Studio 代码检查报告
- excel - VBA 通过选择启用功能区按钮
- javascript - 创建元素上的事件
- php - 未收到通知回调
- python - 使用正则表达式获取熊猫数据框列中部分匹配的行索引
- javascript - 纯Javascript中的字母数字哈希函数
- python - 使用 matplotlib.finance 中的 candlestick_ohlc 函数在 python 中绘制 OHLC 蜡烛