我正在尝试在用户注册后添加一些Flash消息
$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.');
});
然而,在我被重定向到登录页面后,我没有看到任何消息。谁知道原因?
有一个显示消息的视图助手
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;
}
}
在其他页面上,Flash消息可以正常工作。
谢谢!
答案 0 :(得分:1)
我发现了自己的错误。我创建了新的信使,而不是通过服务定位器获取它。
$messenger = new FlashMessenger();
应该是
$messenger = $this->sm->getServiceLocator()->get('ControllerPluginManager')->get('flashMessenger');