FlashMessages don't work after redirect

josef.sabl
Member | 153
+
0
-

Hey guys, I read through all the post about this topic but to no avail. My problem is flash messages don't show after redirect (They work well without redirecting).

I don't know where to look. The app is relatively clean sandbox nothing special.

My URL after redirect looks like this: http://localhost:88/?_fid=sdz1

And bdump of session on target page is here.

I don't know where to look and what piece of code to post.

I generate flash messages as simply as this:

$this->flashMessage('foo');
$this->redirect(":Homepage:Index:default");

I am on 3.0 beta.

Roman Halaxa
Member | 60
+
0
-

Hi. Do you have code to draw them in you Homepage:index:default latte file ? ?

taken from docs

{foreach $flashes as $flash}
    <div class="flash {$flash->type}">{$flash->message}</div>
{/foreach}

Make sure you have something like this in your template file of the page you're redirecting to

josef.sabl
Member | 153
+
0
-

Yes, sure, I have that code in my layout that is extended by action templates. Just to make sure the template is fine I tried this action:

public function actionDefault()
{
    $this->flashMessage('foo');
}

and the flash message is rendered.

Roman Halaxa wrote:

Hi. Do you have code to draw them in you Homepage:index:default latte file ? ?

taken from docs

{foreach $flashes as $flash}
    <div class="flash {$flash->type}">{$flash->message}</div>
{/foreach}

Make sure you have something like this in your template file of the page you're redirecting to

David Grudl
Nette Core | 8218
+
0
-

I think the best way is to step through the code and see why it does not pass session data to the template.

josef.sabl
Member | 153
+
0
-

Yeah, I love Xdebug, but in this case it is a bit problematic, with the nature of the flashmessages (session, redirects, 3 seconds expiration), but I'll try harder ;-)

josef.sabl
Member | 153
+
0
-

Looks like: \Nette\Application\UI\Component::getParameter returns false ($this->params === []) even if I can see the parameters in the URL. Will try to look further into this.

josef.sabl
Member | 153
+
0
-

However when I bdump $this->getParameters in the action code. It contains the params. Looks like something is called in the wrong order.

josef.sabl
Member | 153
+
0
-

Hah, found it!

I had a presenter code like this:

/**
 * @param \Nette\Localization\ITranslator $translator
 */
public function injectTranslator(\Nette\Localization\ITranslator $translator): void
{
    assert($this->template instanceof \Nette\Bridges\ApplicationLatte\Template);
    $this->template->setTranslator($this->_internal_translator);
}

… and changed it to:

/**
 * @var \Nette\Localization\ITranslator $translator
	 * @internal
 */
private $_internal_translator;

public function beforeRender() {
    assert($this->template instanceof \Nette\Bridges\ApplicationLatte\Template);
    $this->template->setTranslator($this->_internal_translator);
}

/**
 * @param \Nette\Localization\ITranslator $translator
 */
public function injectTranslator(\Nette\Localization\ITranslator $translator): void
{
    $this->_internal_translator = $translator;
}

Thanks @dg for extra motivation!!!

Btw, isn't there a way to register translator to template factory somehow? Or what is the best practice for setting the system-wide translator? I hate setting translator for presenter, email templates and maybe even form, separately.

Last edited by josef.sabl (2019-03-12 16:54)