ZfcUser - flash messages after user registration

298 views Asked by At

I'm trying to add some flash messages after user registered

$sharedManager->attach('ZfcUser\Service\User',  'register.post', function($e) use($serviceManager) {
    $user = $e->getParam('user');
    $mail = new Mail\ActivateAccount($serviceManager, $user);
    $mail->send($user->getEmail());
    $flash = $serviceManager->get('ControllerPluginManager')->get('flashMessenger');
    $flash->addSuccessMessage('Check your mailbox, please.');
});

However, after I was redirected to login page I didn't see any message. Anybody knows the reason?

There is view helper that displays messages

namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;
use Zend\View\Helper\FlashMessenger;

class ShowMessages extends AbstractHelper
{
    public function __invoke()
    {
        $messenger = new FlashMessenger();
        $error_messages = $messenger->getErrorMessages();
        $messages = $messenger->getSuccessMessages();
        $result = '';
        if (count($error_messages)) {
            foreach ($error_messages as $message) {
                $result .= '<p class="alert alert-danger">' . $message . '</p>';
            }
        }

        if (count($messages)) {
            foreach ($messages as $message) {
                $result .= '<p class="alert alert-success">' . $message . '</p>';
            }
        }

        return $result;
    }
}

On other pages flash messages works fine.

Thanks!

1

There are 1 answers

0
Grigory On BEST ANSWER

I found my mistake. I created new messenger instead of getting it throuhg service locator.

$messenger = new FlashMessenger();

should be

$messenger = $this->sm->getServiceLocator()->get('ControllerPluginManager')->get('flashMessenger');