Reputation: 73
I'm getting following error in my ZF2 FlashMessenger:
Call to a member function toArray() on array in /vendor/zendframework/zend-mvc/src/Controller/Plugin/FlashMessenger.php on line 306
FlashMessages will be rendered by my FlashMessageHelper:
namespace Application\View\Helper;
use Zend\View\Helper\AbstractHelper;
use Zend\View\Helper\FlashMessenger;
class FlashMessageHelper extends AbstractHelper {
public function __invoke(FlashMessenger $flashMessenger) {
$flashMessenger->setMessageOpenFormat('<div%s><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button><p class="text-center">');
$flashMessenger->setMessageSeparatorString('</p><p class="text-center">');
$flashMessenger->setMessageCloseString('</p></div>');
$flashMessenger->setAutoEscape(false);
echo $flashMessenger->render('error', array(
'alert',
'alert-dismissible',
'alert-danger'
));
echo $flashMessenger->render('info', array(
'alert',
'alert-dismissible',
'alert-info'
));
echo $flashMessenger->render('default', array(
'alert',
'alert-dismissible',
'alert-warning'
));
echo $flashMessenger->render('success', array(
'alert',
'alert-dismissible',
'alert-success'
));
}
}
?>
Does anybody know about this error? This is first time, that this error occured. Google and "zend-mvc" - repo - issues says nothing about this.
Thanks for your attention!
Upvotes: 0
Views: 1808
Reputation: 31
See this in ZF2 (Best Pratices):
The Plugin FlashMessenger , send your message to a waiting pool ( Through FlashMessenger Zend MVC Plugin ) which will be displayed on another page request ( Through ViewHelper FlashMessenger ) .
There are 4 types of messages that you can integrate with the Bootstrap Notifications ( error, info , default , success ) .
Now let's practice
In Action within the Controller , you must enter your message and your brand :
use Zend\Mvc\Controller\Plugin\FlashMessenger;
public function registerAction(){
if($formValid){
$this->flashMessenger()->addSucessMessage('Saved!');
} else{
$this->flashMessenger()->addErrorMessage('Fail!');
}
//redirect to other route and show message
return $this->redirect()->toRoute('app');
}
In View ( .phtml ) , you only need to use :
#show messages of addErrorMessage();
echo $flash->render('error', array('alert', 'alert-dismissible', 'alert-danger'));
#show messages of addInfoMessage();
echo $flash->render('info', array('alert', 'alert-dismissible', 'alert-info'));
#show messages of addMessage();
echo $flash->render('default', array('alert', 'alert-dismissible', 'alert-warning'));
#show messages of addSucessMessage();
echo $flash->render('success', array('alert', 'alert-dismissible', 'alert-success'));
In View , if using Bootstrap :
$flash = $this->flashMessenger();
$flash->setMessageOpenFormat('<div>
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">
×
</button>
<ul><li>')
->setMessageSeparatorString('</li><li>')
->setMessageCloseString('</li></ul></div>');
echo $flash->render('error', array('alert', 'alert-dismissible', 'alert-danger'));
echo $flash->render('info', array('alert', 'alert-dismissible', 'alert-info'));
echo $flash->render('default', array('alert', 'alert-dismissible', 'alert-warning'));
echo $flash->render('success', array('alert', 'alert-dismissible', 'alert-success'));
Now 's a hack , if you want to view the FlashMessages on the screen without resquest ou redirect page ( Ideal for form errors , which you do not redirects or AJAX to another page ) , use this technique .
public function registerAction(){
if($formValid){
$this->flashMessenger()->addSucessMessage('Saved!');
} else{
#add to pool
$this->flashMessenger()->addErrorMessage('Fail');
#merge message
$feedback = array_merge(
$this->flashMessenger->getErrorMessages(),
$this->flashMessenger->getCurrentErrorMessages()
);
#clear pool
$this->clearCurrentErrorMessages();
}
#message in variable to VIEW
new ViewModel(array(
'feedback' => $feedback,
));
}
}
If you want to deepen better at it, follow the links Official Zend 2 documentation , gives a tried on available methods , will help a lot :
VIEW -> http://framework.zend.com/manual/current/en/modules/zend.view.helpers.flash-messenger.html
CONTROLLER -> http://framework.zend.com/manual/current/en/modules/zend.mvc.plugins.html#zend-mvc-controller-plugins-flashmessenger
Upvotes: 1