Working with Zend Framework Flashmessenger and Jquery UI State Classes

Working with Zend Framework FlashMessenger and jQuery UI state classes

Having written the Noumenal FlashMessenger View Helper I should be able to help. :-)

To answer your question:

Adding Messages

You can set different message levels, e.g. error, warning etc, by passing an array to the FlashMessenger Action Helper rather than a simple string:

// ExampleController.php
$this->_helper->FlashMessenger(
array('error'=>'Sorry, could not complete your request.')
);

The view helper is designed to recognise this.

Outputting Messages

When outputting FlashMessages in your layout, there are optional parameters that you can pass to specify the default message level (which is warning by default) and a template for your message.

Adapting your code snippet to account for differing message levels you could achieve the desired result by making the following call in your layout:

// layout.phtml
$template = '<div class="ui-state-error ui-corner-all">
<p class="%s"><span class="ui-icon ui-icon-alert"></span>
<span class="flash-message">%s</span></p>
</div>';
echo $this->flashMessenger('error', $template);

(You may find it better to set the template as a view variable in, say, your bootstrap.)

Doing this the view helper will construct the appropriately formatted flash messages for you as you wish.

Some Simple Styling

Using CSS there would be plenty of room to style the messages appropriately. For example:

.alert {
color: red;
}

.alert .flash-message:before {
content: "<strong>Alert</strong> ";
}

.notice {
color:yellow;
}

.notice .flash-message:before {
content: "<strong>Notice</strong> ";
}

I leave you to improvise...

I wrote a guide to Zend Framework FlashMessenger and the view helper on my blog. Perhaps give that a read. Also please do email me to let me know your difficulties -- it will help me know what I need to improve.

I hope that helps.

Trigger Zend FlashMessenger in jQuery

The trick to your answer lies at your view scripts (*.phtml files).

I am assuming the following index.phtml file as a view script of your indexController file that prints the form and execute your Ajax. When you click the button 'Get External Content', the result of the Ajax response from /index/line controller will be populated in the div1 tag.

    <!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){

$.ajax({url: '/index/line',
success: function(res){
$("#div1").html(res);
//your FlashMessenger will execute from here with AJAX success.
}
});
});
});
</script>
</head>
<body>
<?php // print your form from controller here. echo $this->form; ?>
<div id="div1">Let jQuery AJAX Change This Text</div>
<button>Get External Content</button>
</body>
</html>

The above code will fetch the result from lineAction at indexController ('/index/line'). Add the following lines at IndexController.php

public function lineAction() 
{
$this->_helper->FlashMessenger('Succesfully added');

if ($this->_helper->FlashMessenger->hasMessages())
{$this->view->messages = $this->_helper->FlashMessenger->getMessages();}
}

Now call the view script of your line action (line.phtml). Add the following lines in line.phtml

<?php  
if ($this->messages!=Null)
{ $messages = $this->messages;
foreach($messages as $message) {
echo '<script> alert("'.$message.'") </script>';
}
}

Now execute your code. Your Ajax will execute Zend FlashMessenger with success and display an JavaScript alert message 'Succesfully added'.

I have tested the above code and it works great with Zend 1.12.

FlashMessenger in Zend 2

i just wrote my own simple viewhelper:

<?php

namespace My\View\Helper;

use Zend\View\Helper\AbstractHelper;

class FlashMessenger extends AbstractHelper
{
protected static $_flashMessenger;

public function __invoke($namespace = 'default') {

if (!self::$_flashMessenger) {

self::$_flashMessenger = new \Zend\Mvc\Controller\Plugin\FlashMessenger;
}

return self::$_flashMessenger->setNamespace($namespace);
}
}

use it like:

<? if ($this->flashMessenger()->hasMessages()): ?>
<ul>
<? foreach ($this->flashMessenger()->getMessages() as $message): ?>
<li><?= $message></li>
<? endforeach ?>
</ul>
<? endif ?>

Zend Framework: Method after controller action, but before view rendered

The easiest way to do this is with a controller plugin, see http://framework.zend.com/manual/1.12/en/zend.controller.plugins.html. The postDispatch() method runs after your controller code but before the page is rendered.

How to use multiple jQuery UI Themes in a Zend Framework application?

You can do this using CSS scope. When you build your theme on the jQuery UI site choose Advanced Settings and then enter in name in the scope field.

Do the same with whatever other themes you want to use, making sure that the scope is unique for each one.

Include the CSS theme files as normal (using appendStylesheet) and use the scope classes to style each area/item on the page

A styled example is given here

Type of Flash Messenger in Zend

I think the best way to do this is by using the flashmessenger namespaces:

/* success message */
$this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Post created!');

/* error message */
$this->_helper->FlashMessenger()->setNamespace('error')->addMessage('You have no permissions');

And then in your layout you can get the messages added to each namespace:

<?php $flashMessenger = Zend_Controller_Action_HelperBroker::getStaticHelper('FlashMessenger');

<?php if ($flashMessenger->setNamespace('success')->hasMessages()): ?>
<div class="message success">
<?php foreach ($flashMessenger->getMessages() as $msg): ?>
<?php echo $msg ?>
<?php endforeach; ?>
</div>
<?php endif; ?>

<?php if ($flashMessenger->setNamespace('error')->hasMessages()): ?>
<div class="message error">
<?php foreach ($flashMessenger->getMessages() as $msg): ?>
<?php echo $msg ?>
<?php endforeach; ?>
</div>
<?php endif; ?>

Zend framework 1 flash messenger yields empty array

The FlashMessenger helper allows you to pass messages that the user
may need to see on the next request. To accomplish this,
FlashMessenger uses Zend_Session_Namespace to store messages for
future or next request retrieval.

You can see it in the doc

So your message can be recovered in another action (other request).

If you want to retrieve the message in the same action, you can try to use getCurrentMessages():

$this->view->messages = this->_helper->flashMessenger->getCurrentMessages();

But if this message is only for one request, you can use Zend_Registry



Related Topics



Leave a reply



Submit