Pacakage Flash Data for Success/Error Messages
Permalink
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:http://codeigniter.com/user_guide/libraries/sessions.html... (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.
Thanks!
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:http://codeigniter.com/user_guide/libraries/sessions.html... (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.
Thanks!
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).
Hi,
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:
Then somewhere in the footer as you suggested, you could do something like this.
I updated the Github repo at:
https://github.com/spadin/flash_data...
Good luck,
Sandro
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'); $flash_data->discard('notice'); // or to discard all three type: notice, error & alert. $flash_data->discard();
I updated the Github repo at:
https://github.com/spadin/flash_data...
Good luck,
Sandro
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.
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.
This ought to work fine! Thanks for making the change.
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:
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:
http://www.concrete5.org/documentation/how-tos/developers/build-a-s...
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).
-Jordan
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!'); $this->view(); }
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:
http://www.concrete5.org/documentation/how-tos/developers/build-a-s...
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).
-Jordan
http://sa.ndropad.in/flash-data-helper-for-concrete5...
https://github.com/spadin/flash_data...
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.