Pacakage Flash Data for Success/Error Messages

Hi all,

How would I go about passing around success and error messages between single pages of my custom package in the admin dashboard? I don't like to use GET var's very much and prefer a session based solution.

Read about how Codeigniter does it here: (Section on Flashdata)

Does Concrete5 already have something like this or that would be a good solution?

Also, if someone could point out a good reference on how to write a package with single_pages, controllers, et. all that'd be great.


View Replies:
sandropadin replied on at Permalink Reply
Hi, I was needing the same thing and ended up creating a helper that lets you set a notice, error or alert. It's fairly basic, but it might help you. The helper sets a session variable that is then unset when you access it the next time.

I'll probably add it as a free package on the c5 marketplace soon but haven't had time to do that yet.

Good luck.
jamonholmgren replied on at Permalink Reply
Thanks for posting this! Does it clear unused notices on the next page load? That's sometimes helpful so you don't end up with random notices showing up in odd places. Or is there a class method to do that? I like to call the clear in the footer (if the notice hasn't been shown by then, it usually never will be).
sandropadin replied on at Permalink Reply

I'm glad you found this useful. Unfortunately I don't know if it's possible to clear the messages automatically like you mention without directly modifying Concrete5.

I went ahead and added a discard function to the class though so you can now do something like this:

$flash_data = Loader::helper('flash_data','flash_data');
$flash_data->notice('Some message here..');

Then somewhere in the footer as you suggested, you could do something like this.

$flash_data = Loader::helper('flash_data','flash_data');
// or to discard all three type: notice, error & alert.

I updated the Github repo at:

Good luck,
jordanlev replied on at Permalink Reply
Perhaps instead of requiring a manual clear, you could store some kind of history in another session variable, and do "garbage collection" of old messages in the controller's on_start method?

Within your class, you could make a private array called "current_keys", whose values are the keys of messages that were set. When your private "flash" function is called, it also adds the message key to the current_keys array. Then in the controller's on_start method, delete all messages whose keys are *not* in the current_keys array. Then delete all the values of the current_keys array. (Make sure you do it in this order, otherwise you're deleting messages that were just set last time).

Hope that makes sense.
jamonholmgren replied on at Permalink Reply
This ought to work fine! Thanks for making the change.
jordanlev replied on at Permalink Reply
Concrete5 unfortunately doesn't have the "flash message" capability that Rails, CI, etc. do.

When building dashboard pages, the general pattern is to create an action method in your controller that sets the message and then calls the view() method, for example:
public function edit_success() {
  $this->set('message', 'Record successfully saved!');

When you're in the dashboard, you can set the 'message' variable to a string and C5 will automatically output it to the top of the page, or you can set the 'error' variable to an array of strings (or an array of just one string if you only have one message) and it will automatically display that as well.

But it's not storing this in the session so you have to redirect to that other action after your form processing (or whatever it is you're doing).

The best tutorial I've found on creating dashboard pages is this:

That will give you a good idea of the overall structure of things. However, having experienced the joys of other MVC frameworks myself, it's definitely a bit more complex than I like, and over time I've developed my own boilerplate code for these things. This post was from a while ago, so I'm not sure if you're still around, but if so let me know and I can send that to you. I plan to put it up on github soon anyway (was waiting until Concrete5.5 was released, because it fixed a bug that is needed to make my code work).