0

I'd like to setup flash message(slim/flash). I saw the reference here. I made the following middleware to register flash message.

use Interop\Http\ServerMiddleware\DelegateInterface;
use Zend\Diactoros\Response\RedirectResponse;

function($request, DelegateInterface $delegate)
{
    $flash = $request->getAttribute('flash');
    $flash->addMessage('message', 'Hello World!');

    return new RedirectResponse('/other-middleware');
}

and the question is how to get this flash message from view templates? The reference wrote here but I'm not sure where should I put this code and how to show the flash.

use Interop\Http\ServerMiddleware\DelegateInterface;

function($request, DelegateInterface $delegate)
{
    $flash = $request->getAttribute('flash');
    $messages = $flash->getMessages();
    // ...
}

Thank you for your help.

ima0123
  • 85
  • 1
  • 10

1 Answers1

0

The hint is in sentence below that block of code:

From there, it's a matter of providing the flash messages to your template.

You need pass $messages to your view script in order to be able to render them. Something like:

return new HtmlResponse(
  $this->renderer->render(
    $template,
    ['messages' => $messages]
  )
);