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

问题描述 投票:0回答:1

我有一个可以导入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',
]);
php symfony popup twig message
1个回答
0
投票

我知道“ 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;
       ]);
© www.soinside.com 2019 - 2024. All rights reserved.