php - 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',
]);
解决方案
我知道“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;
]);
推荐阅读
- javascript - 消除重复无法按预期工作
- javascript - 如何在 javascript 中生成的动态 html 元素上应用 .css 规则
- c# - 有没有简单的方法可以使用未知字符作为“ sim.Keyboard.KeyPress”的输入?
- java - java.lang.IllegalStateException:应在主线程上调用,但为 RxCachedThreadScheduler-1
- c++ - 为什么 j 需要小于或等于 sqrt(i)?
- aptitude - 能力(基于时间和工作的问题(Wipro)))
- python - 如何将空格分隔的字符串输入作为 OrderedDict 的键?
- python-3.x - python:从外部文件动态加载函数
- django - 限制对 Django 中对象的访问
- python - 如何使用 django 框架为用户提供在 python 中选择登录或注册的选项?