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

Exception or error:

I have a function which can import an excel file, he is in a service class. When the query $result is true or false (the $result is a boolean), i wan’t to send a message like a pop up into the form template.

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

There is the code in service:

$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*/
}

Here the code in controller:

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

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

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

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

I don’t understand this, but try to return your message from service , ex:

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

return $message;

Then from controller …

If you want flash message :

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

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

Or if you want to manipulate the response manually try :

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *