定制 praxustech/php-flash-messages 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

praxustech/php-flash-messages

Composer 安装命令:

composer require praxustech/php-flash-messages

包简介

A modern take on PHP session-based flash messages

README 文档

README

A modern PHP library for session-based flash messages, compatible with Bootstrap alert styles.

Forked and extended from plasticbrain/php-flash-messages.

Requirements

  • PHP >= 5.4.0
  • An active PHP session

Installation

Via Composer:

composer require praxustech/php-flash-messages

Basic Usage

<?php

// Start a session before using the library
session_start();

require_once 'vendor/autoload.php';

use PraxusTech\FlashMessages\FlashMessages;

$flash = new FlashMessages();

// Add messages
$flash->info('This is an info message.');
$flash->success('Operation completed successfully!');
$flash->warning('Please review your input.');
$flash->error('Something went wrong.');

// Display all messages
$flash->display();

Message Types

Method Type constant CSS class applied
info() FlashMessages::INFO alert-info
success() FlashMessages::SUCCESS alert-success
warning() FlashMessages::WARNING alert-warning
error() FlashMessages::ERROR alert-danger

Adding Messages

Shorthand methods

$flash->info('Your profile has been updated.');
$flash->success('File uploaded successfully.');
$flash->warning('Your session will expire soon.');
$flash->error('Invalid credentials.');

Generic method

// $flash->add($message, $type, $redirectUrl, $sticky)
$flash->add('Custom message', FlashMessages::SUCCESS);

Redirecting After Adding a Message

Pass a URL as the second argument to any method. The user will be redirected immediately after the message is queued in the session.

$flash->error('You must be logged in.', '/login');

$flash->success('Profile saved!', '/dashboard');

// Also works with the generic add() method
$flash->add('Account created!', FlashMessages::SUCCESS, '/welcome');

Sticky Messages

Sticky messages do not show a close button. Use this for important notices that should stay visible.

// Using the sticky() method (defaults to INFO type)
$flash->sticky('This is a sticky info message.');

// Pass a type as the third argument
$flash->sticky('Critical error occurred.', null, FlashMessages::ERROR);

// Or use the $sticky parameter on any method
$flash->warning('Maintenance scheduled tonight.', null, true);
$flash->error('Account suspended.', '/contact', true);

Displaying Messages

Display all messages

$flash->display();

Display only a specific type

$flash->display(FlashMessages::ERROR);

Display multiple specific types

$flash->display([FlashMessages::ERROR, FlashMessages::WARNING]);

Control display order

By default, messages are displayed in this order: error, warning, success, info. Pass an array to override the order:

$flash->display([FlashMessages::SUCCESS, FlashMessages::INFO, FlashMessages::ERROR, FlashMessages::WARNING]);

Return instead of echo

Pass false as the second argument to get the HTML as a string instead of printing it:

$html = $flash->display(null, false);
echo $html;

Checking for Messages

// Check if there are any error messages
if ($flash->hasErrors()) {
    // Handle errors
}

// Check if there are messages of any type
if ($flash->hasMessages()) {
    // There are pending messages
}

// Check for a specific type
if ($flash->hasMessages(FlashMessages::SUCCESS)) {
    // There are success messages
}

Customization

All customization methods return $this, so they can be chained.

Custom HTML wrapper

The wrapper receives two %s placeholders: the CSS class and the message content.

$flash->setMsgWrapper("<div class='%s'><p>%s</p></div>\n");

Prepend/append content to messages

$flash->setMsgBefore('<strong>Notice:</strong> ');
$flash->setMsgAfter(' <em>(please read)</em>');

Custom close button

$flash->setCloseBtn('<button class="btn-close" data-bs-dismiss="alert"></button>');

Custom CSS classes

// Change the base CSS class applied to all messages
$flash->setMsgCssClass('alert dismissible fade show');

// Change the CSS class for sticky messages
$flash->setStickyCssClass('pinned');

// Map message types to custom CSS classes
$flash->setCssClassMap(FlashMessages::ERROR, 'my-error-class');

// Or update multiple at once using an array
$flash->setCssClassMap([
    FlashMessages::SUCCESS => 'toast-success',
    FlashMessages::ERROR   => 'toast-error',
]);

Chaining customizations

$flash
    ->setMsgWrapper("<div class='%s'>%s</div>\n")
    ->setMsgCssClass('alert show')
    ->setCloseBtn('<button class="btn-close" data-bs-dismiss="alert"></button>');

License

Licensed under the Apache 2.0 and MIT licenses.

Original work by Mike Everhart. Extended by Joao Pedro Matias and Bruna Gomes.

praxustech/php-flash-messages 适用场景与选型建议

praxustech/php-flash-messages 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 535 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「bootstrap」 「session」 「flash」 「messages」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 praxustech/php-flash-messages 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 praxustech/php-flash-messages 我们能提供哪些服务?
定制开发 / 二次开发

基于 praxustech/php-flash-messages 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 535
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-06