nassiry/flash-messages
Composer 安装命令:
composer require nassiry/flash-messages
包简介
A PHP package for handling flash messages with session storage & rendering.
README 文档
README
Flash Messages
A lightweight PHP library for handling flash messages with session storage & simple HTML rendering. This package does not depend on any framework, but it can be integrated with frameworks like Laravel, Symfony, CakePHP, and CodeIgniter if needed.
Features
- Session-based flash message storage.
- Instant message rendering.
- Custom message types.
- Fully framework-agnostic.
Table Of Contents
- Installation
- Usage
- Adding Different Types of Messages
- Adding a custom type message
- Testing
- License
- Contributing
Installation
The recommended way use Composer to install the package:
composer require nassiry/flash-messages
Usage
By default, the package stores messages in sessions to be displayed on the next page load.
1. Default Usage (Display on Next Page Load)
require __DIR__ . '/vendor/autoload.php'; use Nassiry\FlashMessages\FlashMessages; // Create an instance $flash = FlashMessages::create(); // Add flash messages to be displayed on the next page load $flash->success('Operation successful!'); $flash->error('An error occurred.'); // Render messages on the next page template file $flash->render();
Note: Ensure that
session_start();has been called in your script before using.
2. Current Page Usage (Display Immediately)
To display a message on the current page, set the second argument to true (default is false):
require __DIR__ . '/vendor/autoload.php'; use Nassiry\FlashMessages\FlashMessages; // Create an instance $flash = FlashMessages::create(); // Add flash messages to be displayed immediately $flash->error('An error occurred.', true); // Render messages instantly in the same page $flash->render();
3. Checking for Messages
You can check if there are any stored messages:
// Check if ANY messages exist if ($flash->hasMessages()) { echo "There are flash messages available."; } // Check if SPECIFIC TYPE messages exist if ($flash->hasMessages('info')) { echo "There are info messages available."; }
4. Retrieving Messages
You can retrieve all stored messages as an array:
// Get ALL messages $allMessages = $flash->getMessages(); foreach ($allMessages as $message) { echo $message['type'] . ': ' . $message['message'] . "<br>"; } // Get messages of SPECIFIC TYPE $infoMessages = $flash->getMessages('info'); if (!empty($infoMessages)) { foreach ($infoMessages as $message) { echo 'Info: ' . $message['message'] . "<br>"; } } else { echo "No info messages found."; }
5. Rendering Messages
Outputs all flash messages with default HTML structure
$flash->render();
<div class="flash flash-success">Operation successful!</div> <div class="flash flash-error">An error occurred!</div>
6. Clearing Messages
To clear all stored messages:
$flash->clear();
Adding Different Types of Messages
Currently, the package support following message types.
$flash->success('Success message!');$flash->error('Error message!');$flash->info('Informational message!');$flash->warning('Warning message!');
Adding a custom type message
addCustomType(string $type, string $message, bool $instant = false)
$flash->addCustomType('notification', 'This is a custom notification message!', true ); $flash->addCustomType('alert', 'This is an alert message!', false );
Custom Rendering Logic
If you need to customize the way messages are displayed, extend the FlashMessageRenderer class:
use Nassiry\FlashMessages\FlashMessageRenderer; class CustomRenderer extends FlashMessageRenderer { public function renderMessage(string $type, string $message): void { echo sprintf('<div class="custom-%s">%s</div>', htmlspecialchars($type), htmlspecialchars($message)); } } $renderer = new CustomRenderer(); $flash = new FlashMessages(new FlashMessageStorage(), $renderer); $flash->success('Custom rendered message!'); $flash->render();
Testing
Run the unit tests to ensure the package works as expected:
1. Prerequisites
Ensure you have all dependencies installed by running:
composer install
2. Running Tests
Execute the following command to run the test suite:
composer test
License
This package is open-source software licensed under the MIT license.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request or open an issue.
nassiry/flash-messages 适用场景与选型建议
nassiry/flash-messages 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 424 次下载、GitHub Stars 达 8, 最近一次更新时间为 2025 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「flash-message」 「session-message」 「php-flash」 「render-flashes」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nassiry/flash-messages 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nassiry/flash-messages 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nassiry/flash-messages 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Modern, elegant toast notifications and confirmation dialogs for Laravel with full Livewire 3.x support, emoji integration, and RTL compatibility
Use it to display a simple flash message with Laravel 5.
Intuitive package to flash notifications on laravel.
CakePHP 3 plugin that adds any flash message to the response.
统计信息
- 总下载量: 424
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-03