php symfony 如何在函数服务中变量为真时创建弹出(树枝)消息?

php symfony How to create a pop up (twig) message when a variable is true in a function service?

我有一个可以导入 excel 文件的功能,他在服务 class 中。当查询 $result 为 true 或 false($result 是布尔值)时,我不想将消息像弹出窗口一样发送到表单模板中。

我知道 "addFlash" 但控制器中没有,但 $result 在控制器调用的服务函数中。

服务中有代码:

$result = mysqli_query($conn, $query);

if ($result == true) {
    /* Here i want to put the confirmation message*/
} else {
    /* Here i want to put the fail message*/
}

这里是控制器中的代码:

$targetPath = '../var/uploads/'.$inputFileName;

$importer->import($targetPath); /*function called of the service*/

return $this->render('upload/form.html.twig', [
    'importResult' => 'ImportController',
]);

I know the "addFlash" but there is not in a controller but $result is in a service function called by a controller

我不明白这一点,但请尝试 return 您来自服务的消息,例如:

if ($result == true){
   $message = 'Your message here';
   }else{
    $message = 'Your fail message here';
}

return $message;

然后从控制器...

如果你想要闪信:

$message = $importer->import($targetPath);
$this->addFlash('message',$message);

return $this->render('upload/form.html.twig', [
        'importResult' => 'ImportController',
       ]);

或者,如果您想手动操作响应,请尝试:

$message = $importer->import($targetPath);

return $this->render('upload/form.html.twig', [
        'importResult' => 'ImportController',
        'message' => $message;
       ]);