|
NAMECatalyst::Plugin::StatusMessage - Handle passing of status (success and error) messages between screens of a web application.SYNOPSISIn MyApp.pm:use Catalyst qr/ StatusMessage /; In controller where you want to save a message for display on the next page (here, once the "delete" action taken is complete, we are redirecting to a "list" page to show the status [we don't want to leave the delete action in the browser URL]): $c->response->redirect($c->uri_for($self->action_for('list'), {mid => $c->set_status_msg("Deleted widget")})); Or, to save an error message: $c->response->redirect($c->uri_for($self->action_for('list'), {mid => $c->set_error_msg("Error deleting widget")})); Then, in the controller action that corresponds to the redirect above: sub list :Path { my ($self, $c) = @_; ... $c->load_status_msgs; ... } And, to display the output (here using Template Toolkit): ... <span class="message">[% status_msg %]</span> <span class="error">[% error_msg %]</span> ... DESCRIPTIONThere are a number of ways people commonly use to pass "status messages" between screens in a web application.
This plugin attempts to address these issues through the following mechanisms:
METHODSload_status_msgsLoad both messages that match the token parameter on the URL (e.g., http://myserver.com/widgits/list?mid=1234567890) into the stash for display by the viewer.In general, you will want to include this in an "auto" or "base" (if using Chained dispatch) controller action. Then, if you have a "template wrapper page" that displays both ""status_msg"" and ""error_msg"", you can automatically and safely send status messages to any related controller action. CONFIGURABLE OPTIONSsession_prefixThe location inside $c->session where messages will be stored. Defaults to ""status_msg"".token_paramThe name of the URL param that holds the token on the page where you want to retrieve/display the status message. Defaults to ""mid"".status_msg_stash_keyThe name of the stash key where "success" status messages are loaded when "$c->load_status_msgs" is called. Defaults to "status_msg".error_msg_stash_keyThe name of the stash key where error messages are loaded when "$c->load_status_msgs" is called. Defaults to "error_msg".Configuration ExampleHere is a quick example showing how Catalyst::Plugin::StatusMessage can be configured in# Configure Catalyst::Plugin::StatusMessage __PACKAGE__->config( 'Plugin::StatusMessage' => { session_prefix => 'my_status_msg', token_param => 'my_mid', status_msg_stash_key => 'my_status_msg', error_msg_stash_key => 'my_error_msg', } ); INTERNALSNote: You normally shouldn't need any of the information in this section to use Catalyst::Plugin::StatusMessage.get_error_msgA dynamically generated accessor to retrieve saved error messagesget_status_msgA dynamically generated accessor to retrieve saved status messagesset_error_msgA dynamically generated accessor to save error messagesset_status_msgA dynamically generated accessor to save status messages_get_cfgSubref that handles default values and lets them be overriden from the MyApp configuration.get_status_message_by_typeFetch the requested message type from the user's sessionset_status_message_by_typeSave a message to the user's sessionload_status_msgsLoad both messages that match the token param (mid=###) into the stash for display by the view.make_status_message_get_set_methods_for_typeCalled at startup to install getters and setters for each type of message (status & error)AUTHORKennedy Clark, hkclark@cpan.orgWith many thanks to Matt Trout (MST) for coaching on the details of Catalyst Plugins and for most of the magic behind the current implementation. COPYRIGHTThis library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |