首页 > 解决方案 > php symfony 如何在函数服务中的变量为真时创建弹出(树枝)消息?

问题描述

我有一个可以导入excel文件的功能,他在服务类中。当查询 $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',
]);

标签: phpsymfonypopuptwigmessage

解决方案


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

我不明白这一点,但尝试从 service 返回您的消息,例如:

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;
       ]);

推荐阅读