Cannot declare class Cake\View\Helper\HtmlHelper

Hi to the community,
I am trying to send an email with Cake\Mailer\Email; and when executing the script, I have the following error:

Cannot declare class Cake\View\Helper\HtmlHelper, because the name is already in use in C:\xampp\htdocs\twynko\src\View\Helper\HtmlHelper.php on line 31

I don’t understand what’s happening? I can see when debuging :

$email = new Email();

            ->from(['' => 'xxxxxxx']) (
            ->subject(__('Votre inscription'))
            ->viewVars(['user' => $user])

(xxxxxxxi have removed the info not useful for the ticket)
I can see that in protected helpers it has already the ‘html’ but I am not doing anything else than just calling email?

UPDATE: i can see that it comes from ->template(‘signup’), if i remove it, no more error…but still don’t understand why

Thanks for help.
Kind regards

Can anybody help

What does your signup template look like?

Hi Zuluru

Bonjour <?= $user['firstname'] ?>,

Merci pour votre inscription sur TWYNKO

Vous pouvez valider votre inscription en cliquand sur le lien ci-dessous:

<?= $this->Html->url('Validation',array('controller' => 'users','action' => 'validate', $user['id'],$user['token']',_full' => true)); ?>

Merci à vous

L'équipe TWYNKO

I saw that template was deprecated and must use setTemplate instead and setLayout but also with that, it keeps bugging

But it might come from the content, you might have guess something, because if i set empty content to signup template, it keeps doing the error but the email goes through…

Grrr looks like those long hours lost on a stupid issue I guess

Thanks for commenting

That looks okay. I notice that you’re on Windows, where case sensitivity is not part of the file system. It’s possible that you’ve indicated in one place that you want the “html” helper and in another the “Html” helper? Those could be seen as two different things by the class registry, but both try to load the same file, resulting in what you’re seeing.

The suggestion is very good and clever but I did not call the html nore the Html helper directly, i just used it in the views for the moment.

But the suggestion is very clever…
Tried to search vscode for Html and html related to helpers and seems it is declared everywhere (in source code ) as Html…

I noticed that even if sending the email, when i include a body in the send(’…’) it does not get inside the email…

Those are the king of bugs pissing me off :slight_smile:(


You had said “I can see that in protected helpers it has already the ‘html’”. If it really is “html” in that list instead of “Html”, then that’s likely a problem. (If it really is “Html”, then let this be a lesson in being as precise as possible when posting. :slight_smile: )

To figure out what’s going wrong, you might put something (a break point, if you’ve got an integrated debugger, or a dump of the stack trace otherwise) in the constructor for the HtmlHelper class, that would let you see where the first instance is being created from, which could shed light on what’s going wrong. Remember, CakePHP is all written in PHP, and can be debugged just like your own code can.

In the protected it is ‘Html’, so something is trying to write in this before sendiing the email…
the line 31 of the HtmlHelper is really at the class calling, so it makes senses…

Yes indeed before sending the email and especially before sending the content to layout/template default or any other, it breaks before adding the content to the email which also makes senses, soemthign is trying to call html helpers (or Html helpers) before setting the content

If putting a break point in the HtmlHelper constructor doesn’t narrow it down for you, search all your code for $this->html, case sensitive.

I am a shame, I am playing with stuff maybe I am not good enough :frowning: I was very good in cakephp 2 few years ago, no moved to 3.8 and I have the feeling of being a dumb :frowning:

I went from 1.3 straight to 3, and it was a big change for sure. Don’t be afraid to dig right into the Cake code (except maybe the internals of the ORM, that’s black magic!) to figure out what’s going on.

it comes from this method , $email->viewBuilder()->setTemplate(‘signup’); when removing, it sends the body and does not trigger the error…

That’s where the error happens. But the error you get indicates that it’s trying to re-declare the HtmlHelper class. That means that something is creating an erroneous internal state prior to this, and the error is just an eventual by-product of that. You need to find the first spot that’s referencing the HtmlHelper.

I am afraid that all Mailer is bugged, here is the new bug:
Fatal error : Cannot declare class Cake\Mailer\Transport\SmtpTransport, because the name is already in use in C:\xampp\htdocs\twynko\src\Mailer\Transport\SmtpTransport.php on line 26

I have started from scratch with new installation and working fine, just don’t undestand what had happened, definitely it has to be something in my code I guess

Fatal error : Cannot declare class Cake\View\Helper\HtmlHelper, because the name is already in use in C:\xampp\htdocs\twynko\src\View\Helper\HtmlHelper.php on line 31

ok back i am tired, i almost did not touch nothing just one controller, one view, one entity, one table, 3.8.10 is bugging i am afraid

What exactly did you change in that controller, view, entity and table? Seems that somewhere in those changes must be the problem. Others are using this version without problem.

ok be honest I am not good in cakephp 3 , so what i did I took the mailer folder from the src files in my project src, seems it comes from there… shame on me…but there was no need for that…

So it’s all working again?

Yes until next stupid movement from myself :slight_smile: …thank you very much for taking time to follow this…Really appreciated