flashMessage & redirectUrl not working

pn-pm
Member | 13
+
0
-

hi, i intend to use one action in presenter from multiple other actions (link to it), and if something is not ok (some link parameters may not be ok) i intend to link back to the origination action + flashMessage

wanted use redirectUrl instead of classic nette redirect- to know where from the request arrived, want to use HTTP_REFERER

In the same code (using nette/application v 3.1.110) if i use redirectUrl, then no flash message is transferred
(its redirecting to the same presenter, only different action)

this case, no flashMessage transferred

$this->flashMessage('Something went wrong, maybe no valid data to produce');
$this->redirectUrl($_SERVER['HTTP_REFERER']); // if(!empty($_SERVER['HTTP_REFERER'])...

but if i do this, flashMessage transfers ok:

$this->flashMessage('Something went wrong, maybe no valid data to produce');
$this->redirect('Panel:Action1');

The second case requires me to know what presenter action a linked from (normal link, no form posting etc).

One way i could do, is pass the current presenter action as part of link,
but is it ok, that if using $this->redirectUrl, the flash messages dont work ??? Or is there some bug? or is this a feature?

Last edited by pn-pm (2023-07-10 09:10)

Marek Bartoš
Nette Blogger | 1121
+
0
-

If you want to redirect to the current action, use this.

$this->redirect('this');

Flash messages internally use an url parameter and in case of redirectUrl(), you would have to add it explicitly. Also, always use Nette\Http\IRequest (in presenter available via $this->getHttpRequest()) instead of $_SERVER – it adds some quite important validations.

pn-pm
Member | 13
+
0
-

Marek Bartoš wrote:

If you want to redirect to the current action, use this.

$this->redirect('this');

Flash messages internally use an url parameter and in case of redirectUrl(), you would have to add it explicitly. Also, always use Nette\Http\IRequest (in presenter available via $this->getHttpRequest()) instead of $_SERVER – it adds some quite important validations.

i dont need to redirect to current action, i need to redirect to action, which i linked from (previous action).
about adding the parameter, how do i “add it explicitly” ?

Last edited by pn-pm (2023-07-10 09:23)

Marek Bartoš
Nette Blogger | 1121
+
0
-

There is afaik no api for that, but based on implementation I would try this:

$flashIds = iterator_to_array($this->getFlashSession()->getIterator());
$flashId = $flashIds[array_key_last($flashIds)];
$flashKey = self::FlashKey;
// Add "$flashKey=$flashId" to url query

Last edited by Marek Bartoš (2023-07-10 09:40)

pn-pm
Member | 13
+
0
-

Marek Bartoš wrote:

There is afaik no api for that, but based on implementation I would try this:

$flashIds = iterator_to_array($this->getFlashSession()->getIterator());
$flashId = $flashIds[array_key_last($flashIds)];
$flashKey = self::FlashKey;
// Add "$flashKey=$flashId" to url query

well, your mentioned way, if i dump it:

$flashIds = iterator_to_array($this->getFlashSession()->getIterator());
$flashId = $flashIds[array_key_last($flashIds)];
bdump([$flashIds , $flashId]);

its an array of messages

array
0 => stdClass
  message: '....'
  type: 'info'

no proper flashId string usable to adding to query,

Last edited by pn-pm (2023-07-10 10:07)

Marek Bartoš
Nette Blogger | 1121
+
0
-

Yeah, sorry, it shouldn't be the value of flash message, but the key… This should be right $flashId = array_key_last($flashIds);

Last edited by Marek Bartoš (2023-07-10 10:16)

dakur
Member | 487
+
+1
-

I think the proper way to solve this is to use backlink: https://doc.nette.org/…tore-request

On current page, you store current URL to session with storeRequest() and then when you need to (something is wrong with the params) you “restore” (return to) the previous URL.

Footnote: it can be challenging if you don't want to use session for storing the backlinks or have some value objects in your request, but for basic usage it's totally enough.

pn-pm
Member | 13
+
0
-

Hi, still not right. Anything possibly valid, i only see if I

dump($this->getFlashSession())

then there is a property “name” being 4 letters at end of name, for example

Nette.Application.Flash/49ar

so maybe its those 4 letters?

but adding the getiterator to end, i have array with only one key, being “flash”

alread have change to call the model within same action, to avoid this mess.
but this remains a mystery to me.

Thanks for effort anyway.

Last edited by pn-pm (2023-07-10 10:36)

dakur
Member | 487
+
0
-

@pn-pm See my answer above

Marek Bartoš
Nette Blogger | 1121
+
0
-

That's right, its the part after /

But @dakur is right that storing the whole request may be a better way

pn-pm
Member | 13
+
0
-

Marek Bartoš wrote:

That's right, its the part after /

But @dakur is right that storing the whole request may be a better way

yeah, has few pits to avoid, but i already tried and implemented that way sucessfully.

For case anyone new wishing to do this, dont store the value directly, if you do this

$this->backlink = $this->storeRequest()

..you end up in a redirect loop. Instead i do use it as string in template:

$this->template->backlink = $this->storeRequest()

thanks to @dakur for pointing me to right direction

Last edited by pn-pm (2023-07-10 12:06)