plasticbrain / php-flash-messages
PHP会话基于的闪存消息的现代实现
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-14 18:20:30 UTC
README
将消息存储在会话数据中,直到它们被检索。具有PSR-4兼容性、Bootstrap兼容性、粘性消息等功能。
更多信息请访问 http://mikeeverhart.net/php-flash-messages。
注意
这是对https://github.com/plasticbrain/php-flash-messages-legacy的更新版本。这个更新的版本已经被完全重写,因此与原始版本不兼容!
感谢使用旧版本的所有人,特别是那些提供反馈和建议的人!
特性
- 命名空间
- PSR-4自动加载兼容
- 使用composer安装
- 与Bootstrap兼容
- 完全可定制的消息
- 粘性消息
路线图
- 添加自定义消息类型
- 持久消息(显示消息直到手动清除)
安装
使用Composer
composer require plasticbrain/php-flash-messages
不使用Composer
下载 FlashMessages.php 并将其保存到您的项目目录中。
导入文件
require '/path/to/FlashMessages.php';
基本用法
// Start a Session if (!session_id()) @session_start(); // Instantiate the class $msg = new \Plasticbrain\FlashMessages\FlashMessages(); // Add messages $msg->info('This is an info message'); $msg->success('This is a success message'); $msg->warning('This is a warning message'); $msg->error('This is an error message'); // If you need to check for errors (eg: when validating a form) you can: if ($msg->hasErrors()) { // There ARE errors } else { // There are NOT any errors } // Wherever you want to display the messages simply call: $msg->display();
消息类型
信息
$msg->info('This is a info message');
成功
$msg->success('This is a success message');
警告
$msg->warning('This is a warning message');
错误
$msg->error('This is a error message');
消息类型常量
每个消息类型都可以通过其常量来引用:INFO,SUCCESS,WARNING,ERROR。例如
$msg::INFO $msg::SUCCESS $msg::WARNING $msg::ERROR
重定向
在显示消息之前,可以重定向到不同的URL。例如,重定向回表单(并显示错误消息),以便用户可以纠正错误。
执行重定向的方法是将URL作为第二个参数传递
$msg->error('This is an error message', 'http://yoursite.com/another-page');
重定向在附加到消息的消息排队时执行。因此,如果需要多个消息并且需要重定向,则将URL包含在最后一条消息中
$msg->success('This is a success message'); $msg->success('This is another success message'); $msg->error('This is an error message', 'http://redirect-url.com');
粘性消息
默认情况下,所有消息都包含一个关闭按钮。可以移除关闭按钮,从而使消息粘性。要使消息粘性,请将true
作为第三个参数传递
$msg->error("This is a sticky error message (it can't be closed)", null, true); $msg->warning("This is a sticky warning message (it can't be closed)", null, true); $msg->success("This is a sticky success message (it can't be closed)", null, true); $msg->info("This is a sticky info message (it can't be closed)", null, true);
还有一个特殊的方法,恰当地称为sticky()
,可以用来创建粘性消息
$msg->sticky('This is also a sticky message');
sticky()
接受一个可选的第二个参数用于重定向URL,以及一个第三个参数用于消息类型
$msg->sticky('This is "success" sticky message', 'http://redirect-url.com', $msg::SUCCESS); ``` By default, `sticky()` will render as whatever the default message type is set to (usually `$msg::INFO`.) Use the 3rd parameter override this. ## Helper Methods ### `hasErrors()` Check to see if there are any queued `ERROR` messages. ````php if ($msg->hasErrors()) { // There are errors, so do something like redirect }
hasMessages ( [string $type] )
// Check if there are any INFO messages if ($msg->hasMessages($msg::INFO)) { ... } // Check if there are any SUCCESS messages if ($msg->hasMessages($msg::SUCCESS)) { ... } // Check if there are any WARNING messages if ($msg->hasMessages($msg::WARNING)) { ... } // Check if there are any ERROR messages if ($msg->hasMessages($msg::ERROR)) { ... } // See if there are *any* messages queued at all if ($msg->hasMessages()) { ... }
检查是否有任何特定的消息类型(或任何消息)排队。
setCloseBtn ( string $html )
$msg->setCloseBtn('<button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">&times;</span> </button>')
设置显示在(非粘性)消息上的关闭按钮的HTML。
setCssClassMap ( array $cssClassMap )
$msg->setCssClassMap([ $msg::INFO => 'alert-info', $msg::SUCCESS => 'alert-success', $msg::WARNING => 'alert-warning', $msg::ERROR => 'alert-danger', ]);
设置用于每个特定消息类型的CSS类。
setMsgAfter ( string $msgAfter )
在消息(但在包装器内部)之后添加一段文本(HTML或其他)。
$msg->setMsgAfter('</p>')
setMsgBefore ( string $msgBefore )
在消息(但在包装器内部)之前添加一段文本(HTML或其他)。
在消息(但在包装器内部)之后添加一段文本(HTML或其他)。
$msg->setMsgBefore('<p>')
setMsgCssClass ( [string $cssClass] )
设置应用于所有消息的CSS类,无论其类型如何。
$msg->setMsgCssClass('alert')
setMsgWrapper ( string $html )
设置围绕每个消息的HTML。HTML应包括两个占位符(%s
)用于CSS类和消息文本。
$msg->setMsgWrapper("<div class='%s'>%s</div>")
setStickyCssClass ( [string $cssClass] )
设置用于粘性消息的CSS类
$msg->setStickyCssClass('sticky')
许可证
MIT许可证(MIT)
版权所有(c)2015 Mike Everhart & PlasticBrain Media LLC
在此特此授予任何人免费获得本软件及其相关文档文件(“软件”)的副本的权利,无限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许获得该软件的人这样做,前提是遵守以下条件:
上述版权声明和本许可声明应包含在软件的所有副本或实质部分中。
软件按“原样”提供,不提供任何形式的质量保证,明示或暗示的,包括但不限于适销性、针对特定目的的适用性和非侵权性保证。在任何情况下,作者或版权持有人不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他方式,无论源于、因之或与软件或软件的使用或其他交易有关。