2012/8/24 Ralf Eggert <[email protected]>

> Hi Demian,
>
> thanks again and I understand your reservation. Actually I won't
> probably need it.
>
> I will pass the FlashMessenger to the View Model and pass other messages
> as well. My view helper then takes all these messages and echoes them.
> Like you I want them to be printed always on the same position of my
> layout.


You might use a view helper for this. In your view, you can do something
like this:

$messenger = $this->flashMessenger();

To get a FlashMessenger instance. The helper would look like this then:

use Zend\View\Helper\AbstractHelper;
use Zend\Mvc\Controller\Plugin\FlashMessenger as Plugin;

class FlashMessenger extends AbstractHelper
{
  protected $plugin;

  public function __construct(Plugin $plugin)
  {
    $this->plugin = $plugin;
  }

  public function __invoke()
  {
    return $this->plugin;
  }
}

You have to make a factory for this plugin, which might look something like
this:

'factories' => array(
  'flashMessenger' => function($sm) {
    $plugin = $sm->get('ControllerPluginManager')->get('flashmessenger');

    $helper = new FlashMessenger($plugin);
    return $helper;
  }
)

Of course you can add more logic to the view helper so you get directly all
messages from a given namespace:

public function __invoke($namespace = null)
{
  if (null !== $namespace) {
    $this->plugin->setNamespace($namespace);
  }
  return $this->plugin->getMessages();
}
-- 
Jurian Sluiman

Reply via email to