MMessenger
Overview
MMessenger is a PHP class that allows displaying messages to users in a simple and efficient way. It is particularly useful for displaying feedback after user actions, such as confirmation messages, or error messages. It is especially useful for separating logic from display, allowing you to generate messages on one side and display them where needed.
Features
- Display different types of messages (success, error, warning, information)
- Message grouping for contextual display
- Session-based message storage for display on the next page
- Integrated CSS styling for immediate display
- Support for custom options per message
Basic Usage
Warning
MMessenger uses PHP sessions to store messages.
A session must have been started before using the class.
Sending Messages
Generic Method
MMessenger::send('message_type', 'Your message here', 'optional_group', $options);
Specific Methods by Message Type
// Success message
MMessenger::sendSuccess('Operation completed successfully!');
// Error message
MMessenger::sendError('An error occurred during processing.');
// Warning message
MMessenger::sendWarning('Please verify your information.');
// Information message
MMessenger::sendInfo('Your request has been recorded.');
Displaying Messages
To display messages, call the displayMessages() method where you want them to appear in your layout:
// Display messages from the default group
MMessenger::displayMessages();
// Display messages from a specific group
MMessenger::displayMessages('my_group');
Advanced Usage
Message Groups
Groups allow organizing messages by context and displaying them in different places of your application:
// Send a message in a specific group
MMessenger::sendSuccess('Article saved successfully', 'admin_articles');
// Display only messages from this group
MMessenger::displayMessages('admin_articles');
Custom Options
You can associate additional data with your messages:
// Send a message with options
$options = [
'article_id' => 42,
'action' => 'deletion',
'timestamp' => time()
];
MMessenger::sendInfo('Article modified', 'admin_articles', $options);
// Retrieve messages with their options
$messages = MMessenger::getMessages('admin_articles');
foreach ($messages as $message) {
echo $message['message']; // The message
print_r($message['options']); // The associated options
}
Warning
If you add options to your messages, these options will not be displayed by using the displayMessages() method. You will need to retrieve your messages using the getMessages() method, then handle the display of these options yourself.