How do we use more than one message in flashMessenger using zend framework
I am using zend. I have the following piece of code,
....
$cust = 'test@test.com';
$list.='Also Sent Mail to following members';
foreach($m_list as $value)
{
$mail_to_manu = new Zend_Mail('utf-8');
$mail_to_manu->clearFrom();
$mail_to_manu->setBodyHtml('Text')
->setFrom('noreply@test.co开发者_运维百科m', 'test admin')
->addTo($value['email'])
->setSubject('Test');
$mail_to_manu->send();
$list.=$value['manufacturers_email'].'<br/>';
}
$this->_helper->flashMessenger->addMessage('Mail send to '. $cust. ' Successfully'.$list);
$this->_redirector->gotoUrl('/index');
.....
I got message with out any break.my message looks like,
Mail send to test@test.com Successfully Also Sent Mail to following members some1@example.com some2@example.com...
I need to my message will be like,
Mail send to test@test.com Successfully
Also Sent Mail to following members,
some1@example.com
some2@example.com
...
So i need some break one after another.Is it possible to do that in Flash messenger. If yes,Kindly Advice.
Are you using strip_tags
or something similar in the view script? It could cause the <br />
tags to get stripped out.
It's also possible to add multiple messages by calling flashMessenger->addMessage()
once for every address:
$cust = 'test@test.com';
$this->_helper->flashMessenger->addMessage('Mail send to '. $cust. ' Successfully');
if(count($m_list)>0 )
$this->_helper->flashMessenger->addMessage('Also Sent Mail to following members');
foreach($m_list as $value)
{
$mail_to_manu = new Zend_Mail('utf-8');
$mail_to_manu->clearFrom();
$mail_to_manu->setBodyHtml('Text')
->setFrom('noreply@test.com', 'test admin')
->addTo($value['email'])
->setSubject('Test');
$mail_to_manu->send();
$this->_helper->flashMessenger->addMessage($value['manufacturers_email']);
}
$this->_redirector->gotoUrl('/index');
Your question
The reason why it's all bunched up together is that you're echo
ing it all out in a loop without additional markup.
What about something like:
foreach ($this->messages as $message)
{
echo '<p class="message">' . $this->escape($message) . '</p>';
}
But, in fact, there is a much better way of handling the flashMessenger in views. As you know the Zend FlashMessenger is an action helper. But there is also a great view helper avaiable which helps you output your messages nicely. The best thing about it is, that you can pass an array('warning' => 'This is a warning')
and the array key (warning) will be used as class for the <p>
tag. You can find information about this helper on Carlton Gibson's blog and additional explanations in this SO question.
Your variable naming needs improvement
- Write readable variable names like
$customer
instead of just$cust
. Nobody ever said shortening variable names is a means of writing lean code ;). Shortening variable names is bad (code smell) because it make code less readable for others and for yourself in the future. - Use casing like this
$mailToManufacturer
instead of using underscores. It's a general agreement (standard) and therefore good for readability and understanding of code too.
精彩评论