Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Outputting error, info and success flash message
  • If i`m posting on the wrong board please move it. I`m a fan of pyroCMS and i discovered that flash messages are handle using a single function, i looked for the source but could not find it so i decided to do something on my own. i added a static method to Controller_Base as follows
    public static function FlashMessage($msg)
     {    
        $output = '';
        $messages = explode(" ", $msg);
     
        foreach($messages as $message)
        {
        $flashmessage = Session::get_flash($message);
        if($flashmessage)
        { 
        $output .= "<div class=\"alert-message $message\">";
       if(is_array($flashmessage))
          $output .= implode("<br />", $flashmessage);
       else
          $output .= $flashmessage;
       $output .= "</div>"; 
       
           }  
    
        }  
        return $output;
     }
    

    then in my view i can call it like this
    <?php echo Controller_Base::FlashMessage('success error info'); ?>
    

    Note: In codeigniter this could possibly be a helper function but i`m still new to FuelPHP, if you have a better idea kindly share with me.
  • If you have many base templates, your application design is wrong. :) If your app is that complex, use the Theme class to bring some order to the chaos. I have apps with hunderds of views, but only a few page templates, which are completely partial based. It may be more lines, but it's a more structural solution. Every app needs message management. This just generates a simple and standardized div structure that can be styles with css and js. Once written all I have to do is slow the class into place and I'm done. It will also make all my modules portable, as they all interact with the user in the same way. Which means my time to market will improve, which in turn means I make more money.
  • Our depot project uses a dedicated Messages class for this: https://github.com/fuel/depot/tree/1.0/develop/fuel/app/classes/messages And in the view:
    <!-- Begin messages -->
    <?php
    foreach (array('error', 'warning', 'success', 'info') as $type)
    {
        foreach(\Messages::instance()->get($type) as $message)
        {
            echo '<div class="',$message['type'],'-box">',$message['body'],'</div>',"\n";
        }
    }
    \Messages::reset();
    ?>
    <!-- End of messages -->
    

    It allows you to keep HTML out of your controllers (best practice) and into your views, where it belongs. It is also decoupled from the controller, so other classes can generate messages too.
  • Well, i agree but like i said, in codeigniter i will simply write a helper function instead of attaching this little snippet to a class but for my small app, it works. i like your implementation too, just that i have to type more lines. What will happen if you are using many base templates? copy and paste all around?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion