DEV Community

Cover image for Effortless Flash Messages in PHP: A Powerful Package for Session-Based Notifications
A.S Nasseri
A.S Nasseri

Posted on

Effortless Flash Messages in PHP: A Powerful Package for Session-Based Notifications

As developers, we often need a simple way to show users notifications, alerts, or flash messages on a website after a certain action. Whether it's a success message, error, or information alert, implementing flash messages can become repetitive and error-prone. But it doesn't have to be!

Introducing FlashMessages, a lightweight and easy-to-use PHP package that helps you handle flash messages in your web applications with ease. Whether you're building a small app or a large-scale project, FlashMessages provides a seamless way to manage and display session-based notifications, all while being customizable and extendable.

Why Use FlashMessages?

Here’s why FlashMessages stands out:

  • Simple to use: Just a few lines of code to add success, error, info, or warning messages.
  • Customizable: Easily extendable with custom message types. Translator support: Add translation logic to your flash messages.
  • Persistent: Supports session-based storage, ensuring messages persist across multiple page loads.
  • No dependencies: Standalone package that works without any extra libraries or frameworks.

How to Install

FlashMessages is ready to be used in any PHP project. You can install it via Composer, the PHP dependency manager. If you're not using Composer yet, it’s time to start!

Install via Composer:

Run this command in your project’s root directory:

composer require nassiry/flash-messages
Enter fullscreen mode Exit fullscreen mode

Default Usage

require __DIR__ . '/vendor/autoload.php';

use Nassiry\FlashMessages\FlashMessages;

// Create an instance
$flash = FlashMessages::create();

// Standard messages
$flash->success('Operation completed successfully.');
$flash->error('Something went wrong!');
$flash->info('Here is some useful information.');
$flash->warning('Be cautious about this!');

// Custom message type
$flash->addCustomType('notification', 'This is a custom notification!', true);
$flash->addCustomType('success-green', 'This is a green-themed success message!', false);


// Render messages on the next page template file
$flash->render();
Enter fullscreen mode Exit fullscreen mode

This will output HTML for each message added, like this:

<div class="flash flash-success">Operation completed successfully.</div>
<div class="flash flash-error">Something went wrong!</div>
<div class="flash flash-info">Here is some useful information.</div>
<div class="flash flash-warning">Be cautious about this!</div>
<div class="flash flash-notification">This is a custom notification!</div>
<div class="flash flash-success-green">This is a green-themed success message!</div>
Enter fullscreen mode Exit fullscreen mode

Instant vs. Persistent Messages

You can control whether a message is shown immediately or stored for the next page load using the $instant parameter.

  • Instant Message: Use $instant = true to display the message on the current page.
  • Persistent Message: Use $instant = false to store the message in the session, to be rendered on the next page load default is false.
// Shown immediately
$flash->success('This is an instant success message!', true);  
// Stored for next page
$flash->error('This error will be shown on the next page.', false);  
Enter fullscreen mode Exit fullscreen mode

Conclusion

FlashMessages is a simple yet powerful PHP package for managing and displaying flash messages. It’s designed to be lightweight, easy to use, and extendable. Whether you're adding success messages, error notifications, or custom alerts, FlashMessages makes it easy to manage and display them in a consistent way.

You can start using FlashMessages today by installing it via Composer and integrating it into your PHP projects. Check out the repository on GitHub for more details, or contribute to this open-source project!


I hope you find this package useful! Feel free to star the repo, contribute, or open issues if you have any questions or suggestions. Happy coding! 🚀

Top comments (0)