简体   繁体   中英

What is the correct way to show flash message in Zend Framework 2 Expressive

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.

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]
  )
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM