# Laminas Flash messenger is shown twice

**URL:** <https://discourse.laminas.dev/t/laminas-flash-messenger-is-shown-twice/2727>\
**Category:** Components & MVC\
**Tags:** laminas-mvc, laminas-mvc-plugin-flashmessenger\
**Created:** [January 21, 2022, 6:03am UTC](https://discourse.laminas.dev/t/laminas-flash-messenger-is-shown-twice/2727 "2022-01-21T06:03:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebuddy](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ebuddy/32/1536_2.png) [@ebuddy](https://discourse.laminas.dev/u/ebuddy)\
**Post date:** [January 21, 2022, 6:03am UTC](https://discourse.laminas.dev/t/laminas-flash-messenger-is-shown-twice/2727/1 "2022-01-21T06:03:36Z")

</div>

My problem is that when I call flash messenger plugin in my controller, it works. But the same message appears when I open an another page.  
I would like to know how to avoid it?

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [January 21, 2022, 8:19am UTC](https://discourse.laminas.dev/t/laminas-flash-messenger-is-shown-twice/2727/2 "2022-01-21T08:19:22Z")

</div>

> [@ebuddy](#):
>
> My problem is that when I call flash messenger plugin in my controller, it works.

What does it mean that it works? Is there a redirection that leads to a new HTTP request or will the current messages be rendered in the same request?

---

<div class="post-metadata">

**Author:** ![ebuddy](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ebuddy/32/1536_2.png) [@ebuddy](https://discourse.laminas.dev/u/ebuddy)\
**Post date:** [January 21, 2022, 9:21am UTC](https://discourse.laminas.dev/t/laminas-flash-messenger-is-shown-twice/2727/3 "2022-01-21T09:21:25Z")

</div>

Thank you @froschdesign ,  
Let me post a sample of my code to explain to be most clear

Here is for instance in my controller ab action :

```auto
public function listeDesClassesAction()
    {    
        
        $post=$this->post;  
        $mapperClasse = $this->SM->get('TCLASSE'); 
        $hydrator = new ArraySerializableHydrator();
        $classe = new \Commun\Model\Entity\Classe();
        
        $form = $this->formManager->get(\Admin\Form\Parametrage\ClasseForm::class);              
        $form->bind($classe)->setInputFilter($classe->getInputFilter())->setData($post);           
        if(!$post){
            return new ViewModel(['form' => $form,'user' => $this->user]);
        }        
        
        if(isset($post['enregistrer'])){    
            if(!$form->isValid()){
                $this->FLASH()->showMessage($this->TRANSLATOR()->translate('form-contient-donnees-erronees'),'danger');
                return new ViewModel(['form' => $form,'user' => $this->user]);
            }
            $mapperClasse->beginTransaction(); 
            try{                   
                $data = $hydrator->extract($classe);
                $additionnalData = [
                    'dateSaisie' => date('Y-m-d H:i:s'),
                    'idRefUtilisateur' => $this->user->getIdRefUtilisateur(),
                ];
                $mapperClasse->insert($data,$additionnalData);
                $mapperClasse->commit();
            }catch (\Exception $e){  
                $mapperClasse->rollBack(); 
                $this->FLASH()->showMessage($this->TRANSLATOR()->translate($e->getMessage()),'danger');                                               
                return new ViewModel(['form' => $form,'user' => $this->user]);
            }    
            $this->FLASH()->showMessage($this->TRANSLATOR()->translate('form-successful'));
            return $this->redirect()->toRoute('administration-parametrage', ['action' => 'liste-des-classes']);
        }
                
    }

```

The code for redirection is

```auto
$this->FLASH()->showMessage($this->TRANSLATOR()->translate('form-successful'));
return $this->redirect()->toRoute('administration-parametrage', ['action' => 'liste-des-classes']);

```

Here is my view liste-des-classes.phtml;

```auto
<?php
/**
 *
 * @module Application
 * @subpackage View
 * @author Samuel NANGUI <nanguisamuel@gmail.com>
 * @copyright Copyright (c) 2021 Nslabs
 */
$this->headTitle($this->translate('Administration - '.$this->translate('liste-des-classes-title')));
echo $this->partial('partial/commun/header-datatable');
$form->setAttribute('action', $this->url('administration-parametrage', ['action' => 'liste-des-classes']));
$form->prepare();
$classeForm = $form->get('ClasseForm');
?>
      
<div class="container-fluid inner-starter">                                
    <?=$this->partial('partial/commun/view-header',[
        'flash_redirect' => true,
        'titre' => $this->translate('liste-des-classes-title'),
        'description' => $this->translate('liste-des-classes-desc'),
        
    ])?> 
            
    <div class="row-fluid wrapper box-shadow mb-20">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 onglets-container bloc-violet-border mb-30 bloc2 div-datatable">
            <h5 class="titre-datatable-desc-violet"><img class="icon-datatable" src="<?=$this->basePath('/images/classes.svg')?>"><span class="titre-datatable-desc-violet font-14"><?=$this->translate('liste-des-classes-title')?></span></h5>            
            <div class="p-4 responsive mb-5 col-sm-12 table-borderless table-responsive bloc-center nowrap datatable-violet">
                <?=$this->partial('partial/commun/view-rendering-datatable',[
                    'nbreCols' => 7,
                    'tableId' => 'dt-liste-des-classes',
                    'dataType' => ['dataLibre','dataLibre','dataLibre','dataLibre','dataDateTime','dataLibre','dataAction'],
                    'sizeCol' => [15,15,15,10,15,20,10],
                    'colTitle' => ['intitule-abrege','intitule','groupe-classe','ordre','date-saisie','saisie-par','Action'],
                    'dAlign' => ['left','left','left','center','right','left','center'],
                    'bodyClass' => 'font-13'
                    ])?>       
            </div>
        </div>        
    </div>
    
    <?=$this->form()->openTag($form)?> 
    <h6 class="title-header mt-10"><?=$this->translate('ajouter-classe')?></h6>
    <div class="row clearfix">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 mb-30 mt-20">            
            <div class="p-30 box-shadow bloc-vert-border bloc2 form-input">                                                                                                   
                <?=$this->partial('partial/commun/view-rendering-elements',['form' => $classeForm,'colLabel'=>4,'colInput'=>8,'setLabel'=>true])?>                                                           
            </div>
        </div>          
    </div>      
    <div class="mt-4 mb-4">
        <?=$this->partial('partial/commun/view-rendering-buttons',['form' => $form,'buttons' => ['enregistrer','','',''],'col' => 3])?> 
    </div>  
    <?=$this->form()->closeTag();?>
    
</div>

```

The partial view-header.phtml

```auto
<?php if(isset($this->titre)): ?>
<div class="row">
    <div class="col-12">
        <div class="page-title-box d-sm-flex align-items-center justify-content-between">
            <h4 class="mb-sm-0 font-size-18"><?=$this->titre?></h4>
        </div>
    </div>
    <?php if(isset($this->description)): ?>
    <div class="col-12">
        <div class="page-title-box d-sm-flex align-items-center justify-content-between">
            <p class="description-title"><?=$this->description?></p>
        </div>
    </div>
    <?php endif; ?>
</div>
<?php endif; ?>
<?php if($this->flash_redirect): ?>
    <?= $this->flashMessenger()->renderCurrent('default', [], false);?>
<?php endif; ?>

```

Here are my codes blocks

What I’m trying to do is :

When I post the form and process it in the controller, the successful message is shown clearly. And when for instance I refresh the page, the message still appear. When a try again the message now disapear.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [January 21, 2022, 9:53am UTC](https://discourse.laminas.dev/t/laminas-flash-messenger-is-shown-twice/2727/4 "2022-01-21T09:53:28Z")

</div>

You use a custom controller plugin with a custom flash messenger. The original flash messenger [sets (by default) an expiration hops of 1 for the session container](https://github.com/laminas/laminas-mvc-plugin-flashmessenger/blob/795ed4b76265498e6ee8ff2358a36db0fdae3bfa/src/FlashMessenger.php#L149-L157), so that the messages are not transported further than to the next request. Maybe your implementation is different.

And you use `renderCurrent` in your view script for messages that come from the previous request, but the method is intended for [fetching messages from the current request](https://docs.laminas.dev/laminas-mvc-plugin-flashmessenger/controller-plugin/#available-methods).

_(And please reduce your code snippets, there is just too much that is off topic. Then it will be easier to help. Thanks!)_

---

<div class="post-metadata">

**Author:** ![ebuddy](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/ebuddy/32/1536_2.png) [@ebuddy](https://discourse.laminas.dev/u/ebuddy)\
**Post date:** [January 21, 2022, 9:56am UTC](https://discourse.laminas.dev/t/laminas-flash-messenger-is-shown-twice/2727/5 "2022-01-21T09:56:08Z")

</div>

Ok thank you @froschdesign
