Invalid argument supplied for foreach() when generating link
- ydenda
- Member | 21
Hello together,
What am I missing?
when I attempt to produce the account activation link in presenter:
$template->link = $this->link('Front:Sign:verify', $token);
or in template:
<a href="{link //Front:Sign:verify, $token}">activate account</a>
TRACY complains on
/nette/application/src/Application/UI/Presenter.php:1143
with warning: Invalid argument supplied for foreach()
First I thought the problem could be I haven't used FQDN on my test bench to reach the web app, nevertheless even after change to FQDN, I got the same error.
Unfortunately I cannot show you the TRACY output, since currently I have unsupported php version on production site.
Ubuntu 16/04, Apache 2.4.18, php 7.0, Nette 2.4
- idiox
- Member | 14
Hi,
try to delete the comma after “:verify”
<?php
<a href="{link //Front:Sign:verify, $token}">activate account</a>
?>
<?php
<a href="{link //Front:Sign:verify $token}">activate account</a>
?>
<?php
{link Product:show $productId}
?>
Last edited by idiox (2016-11-12 15:24)
- ydenda
- Member | 21
I tried to isolate the problem a bit more…
When I place following code:
$template->link = $this->link('Front:Sign:verify', $token);
in renderDefault method, the link is successfully generated.
Problem occurs, when I place the line in some other method. For better imagination, please see the code of corresponding presenter:
<?php
namespace FrontModule;
use \Nette;
use \Nette\Mail\Message;
use \Nette\Mail\SendmailMailer;
class MailPresenter extends BasePresenter
{
public function sendActivationMail($username, $email, $token) {
$mail = new Message;
$mail->setSubject('Aktivační email');
$mail->setFrom('nobody@nobody.cz', 'Registrace');
$mail->addTo($email, $username);
$template = $this->createTemplate();
$template->setFile(__DIR__ . '/templates/newUser.phtml');
$template->token = $token;
$template->username = $username;
$template->email = $email;
$template->link = $this->link('//:Front:Sign:verify' , $token);
$mail->setHtmlBody($template);
$mailer = new SendmailMailer;
$mailer->send($mail);
return;
}
}
Does anyone have an idea what I've done wrong?
- ydenda
- Member | 21
Beside others, it is passed in constructor to the SignUpFormFactory:
/** @var \FrontModule\MailPresenter @inject */
protected $mailPresenter;
public function __construct(FormFactory $factory, Model\UserManager $userManager, \FrontModule\MailPresenter $mailPresenter)
{
$this->factory = $factory;
$this->userManager = $userManager;
$this->mailPresenter = $mailPresenter;
}
and then reached via
$this->mailPresenter->sendActivationMail($values->username, $values->email, $token);
Obviously it is not the best practice – any hints on this?
- ydenda
- Member | 21
Well, I made things working a bit.
After some time I've found article Link generation in emails with Nette 2.3
So I created /app/model/MailManager.php with following content:
<?php
namespace App\Model;
use \Nette;
use \Nette\Mail\Message;
use \Nette\Mail\SendmailMailer;
class MailSender
{
/** @var Nette\Application\LinkGenerator */
private $linkGenerator;
/** @var Nette\Application\UI\ITemplateFactory */
private $templateFactory;
function __construct(\Nette\Application\LinkGenerator $generator, \Nette\Application\UI\ITemplateFactory $tf)
{
$this->linkGenerator = $generator;
$this->templateFactory = $tf;
}
public function sendActivationMail($username, $email, $token) {
$mail = new Message;
$mail->setSubject('Account activation');
$mail->setFrom('nobody@nobody.cz', 'SignUp - myProject');
$mail->addTo($email, $username);
$template = $this->templateFactory->createTemplate();
$template->setFile(__DIR__ . '/MailTemplates/newUser.phtml');
$template->getLatte()->addProvider('uiControl', $this->linkGenerator); // for Latte 2.4 allows {link} in template
$template->token = $token;
$template->username = $username;
$template->email = $email;
$mail->setHtmlBody($template);
$mailer = new SendmailMailer;
$mailer->send($mail);
return;
}
}
It made the {link} working in the template, nevertheless $this->link is
not available.
For my needs it is sufficient so far.
Thank you guys for spent effort trying to help me.
Hope this helps others…