Learn DooPHP: High performance PHP framework

DooFlashMessenger

Hi guys, DooFlashMessenger is here, in this tutorial I will explain you how can you use it with just 3 lines of code. Same thing you have in class comments.

Well lets begin, we start with calling class:

$flash = new DooFlashMessenger();

Now we must give access to DooFlashMessenger from view, we do that like this:

DooController::view()->flashMessenger = $flash;

When we can access from view to flashMessenger we can display messages from flashMessenger, we do that with displayMesssages() method.
Here is the code you need to have in your template file:

$flash->displayMessages();

Thats about it, now you just need to add some message to flash messenger, message will be stored in session, with next execution of DooFlashMessenger class messages that are stored in session will be printed with displayMessages() method. Calling displayMessages() method will echo all messages that are stored.

Adding messages to flashMessenger is done by calling method addMessage:

$flash->addMessage("This is just test message");

That’s it if you have any questions please do ask.


  • Spanish translation added. Original version at http://eomblog.blogspot.com/2009/11/dooflashmessenger-con-doophp.html

  • detail tutorial please….

  • Is this something like lite version of Doo Logger with addition of session to make it global access ? I think this should be part the Doo::logger instead of a helper.

  • DooFlashMessenger is used when you do some action and then redirect user to another script where that message is shown.

    How it works is that message is stored in session, when script is activated again it reads it from session, this is used mainly for admin panels.

    So for example you make action for editing something and then on end of it you say:

    $this->_flashMessenger->addMessage(”Table [_1] is edited!”, array($tableName));
    $this->_redirect(baseUrl . “edit-tables”);

  • When I want to keep error messages along with warning messages in the flash, and I want the error and warning messages displayed differently on the view, then I need to make two instances with two different namespace (which I thought the code has not completed yet).

    Why don’t you just implement the Ruby on Rails ways of handling flash messages?

  • You dont need to you can add messages and put something like:

    Error|This is error.
    Warning|This is warning.

    And then make your own function that gets array with messages and parse them like you want in view.

  • I use it in maincontroller like this.
    and displaymessages() did not echo the message.

    public function addMessage($message){
    Doo::loadhelper(’DooFlashMessenger’);
    $flash = new DooFlashMessenger();
    $flash->addMessage($message);
    }

    public function displayMessage(){
    Doo::loadhelper(’DooFlashMessenger’);
    $flash = new DooFlashMessenger();
    $this->data['message'] = $flash->displayMessages();
    }

  • You are using it wrong displayMessages function is used for displaying messages it prints messages, if you want to add messages to some variable you can do that like:

    $this->data['messages'] = $flash->getMessages();

You can follow any responses to this entry through the RSS 2.0 feed.