I would like the Twig QR Code generation function to be operational.
Indeed, when I use it in the code below for example:
<div class="titreSep" align="center">CATECHISM MEMBERSHIP</div>
<table width="100%" border="0" style="font-family:Arial, Helvetica, sans-serif;">
<tr>
<td style="text-align: left; vertical-align: top; width: 70%;">
<p>Membership fee: €35</p>
<p>Check payable to: AD34 Jean Baptiste du ?$#@$</p></td>
<td style="text-align: right; vertical-align: middle; width: 30%;">
<img src="{{ qr_code_url('https://example.com/paiement/cotisation') }}" alt="QR Code" style="width: 100px; height: 100px;"/></td> </tr> </table>
I am getting the following error message:
An error occurred in creating the file, check your model: Typed property App\ Service\ DocumentsPDFDisplayService: :$flashBag must not be accessed before initialization
When asking Gemini, Google's AI, what to do, he gives me the following answer, suggesting that the problem is related to the PHP code on the server:
******************************************************************************
This error is not related to the HTML modification you made, but rather to the way your server-side code (probably in PHP with the Symfony framework) manages one of its classes.
The error “Typed Property App\ Service\ DocumentsPDFDisplayService: :$flashbag must not be accessed before initialization” means that:
You have a service class called DocumentsPDFDisplayService.
This class has a property (internal variable) named $flashBag.
This property has been declared with a “type” (e.g. private flashBagInterface $flashBag;).
The code tries to use this $flashBag property before it has been assigned a value. In PHP, a typed property must have a default value or be initialized in the class constructor.
Cause of the error
The line of code that is generating the error is probably in your DocumentsPdfDisplayService.php file, not in your HTML file.
Modifying the HTML code, such as adding the qr_code_url () function, most likely triggered this service which, for some unknown reason, is not properly receiving its $flashBag dependency before being used.
Solution
Since the error occurs in the DocumentsPDFDisplayService service, you need to edit this file.
There are two ways to solve this problem:
Option 1: Initialize the property in the constructor (Recommended)
It is the cleanest and most common solution. You need to make sure that the $flashBag is properly injected and assigned to the property when creating the class.
In your src/Service/DocumentsPdfDisplayService.php file, you should find something that looks like this:
PHP
//File: DocumentsPdfDisplayService.php
namespace App\ Service;
Use... ;
class DocumentsPDFDisplayService
{
private $flashBag;//or private flashBagInterface $flashBag;
public function __construct (flashBagInterface $flashBag)
{
$this->flashbag = $flashBag;
}
//... the rest of your code
}
If the $flashBag is injected via the constructor, make sure it is properly assigned.
Option 2: Make the property “nullable”
If the $flashBag is not always needed for the service and it may be null, you can declare it as “nullable” and give it a default value.
PHP
//File: DocumentsPdfDisplayService.php
namespace App\ Service;
use Symfony\ Component\ HttpFoundation\ Session\ Session\ Flash\ FlashBagInterface;
class DocumentsPDFDisplayService
{
//Make the property nullable and declare it with a null default value
private? flashbagInterface $flashBag = null;
//... the rest of your code
}
Note: Do not adjust your HTML file. The problem is in your PHP code. The Typed Property error is a clear indicator that an object is being called before it is successfully initialized.
If you have access to your logs, it can give you more information.
****************************************************************************
What do you think, could you, with these explanations, make this Twig function efficient?
Thank you so much for your help
·