zf-hipsters / bootstrap-flash-messenger

Simple implementation of ZF2 flash messengers using bootstrap. Contains easy helpers that will save you a LOT of time.
14 stars 1 forks source link

Multiple messages not possible #3

Open scroach opened 10 years ago

scroach commented 10 years ago

Hey, I just tried out your plugin. It works like a charm, except that I was not able to add multiple messages.

I did some investigation and found out that the __invoke method in FlashMessenger.php seems to be the problem. The method reinitializes the Zend FlashMessenger everytime it gets called.

Adding a private member variable for the FlashManager and initializing it only the first time solved the problem for me! Below is the new code I am using right now.

Best regards, Patrick

<?php
/**
 * ZF-Hipsters Bootstrap Flash Messenger (https://github.com/zf-hipsters)
 *
 * @link      https://github.com/zf-hipsters/bootstrap-flash-messenger for the canonical source repository
 * @copyright Copyright (c) 2013 ZF-Hipsters
 * @license   http://www.apache.org/licenses/LICENSE-2.0 Apache Licence, Version 2.0
 */
namespace FlashMessenger\Controller\Plugin;

use Zend\Mvc\Controller\Plugin\AbstractPlugin;

/**
 * Class FlashMessenger
 * @package FlashMessenger\Controller\Plugin
 */
class FlashMessenger extends AbstractPlugin
{
    private $fm = null;
    /**
     * Control Plugin
     *
     * @param $message
     * @param string $namespace
     * @return \Zend\Mvc\Controller\Plugin\FlashMessenger
     */
    public function __invoke($message, $namespace = 'success')
    {
        if($this->fm == null)
            $this->fm = new \Zend\Mvc\Controller\Plugin\FlashMessenger();
        $this->fm->setNamespace($namespace);

        if (is_array($message)) {
            foreach ($message as $msg) {
                $this->fm->addMessage($msg);
            }
        } else {
            $this->fm->addMessage($message);
        }

        return $this->fm;
    }
}
gmuehl commented 10 years ago

Had the same problem and used this patch, works fine, please add it to the Code.