gajus / paddy
基于会话(闪存)的消息工具。
0.1.3
2014-12-04 03:33 UTC
Requires
- php: >=5.4
Requires (Dev)
- psr/log: 1.0.0
- satooshi/php-coveralls: dev-master
This package is not auto-updated.
Last update: 2024-09-14 15:23:08 UTC
README
使用会话(session)来在页面请求之间传输(闪存)消息。
/** * @param string $namespace Namespace is used if more than one application is using Messenger. Defaults to the SERVER_NAME or "default". */ $messenger = new \Gajus\Paddy\Messenger(); /** * @param string $message * @param string $namespace Message namespace (success, error or notice). * @return $this */ $messenger->send('Loaded to the Gunwales!');
发送消息
使用send
方法发送消息。第二个参数用于将消息放入命名空间。
$messenger->send('There is more grog on the deck!', 'success');
命名空间值限于 "error","notice" 和 "success"。限制是为了避免意外的(且难以捕获的)错误。如果您想更改此行为,请提交一个issue,包括替代方案或变量命名空间的示例用例。
获取消息
要获取特定命名空间下的所有消息,请使用getMessages
方法
/** * Return all messages nested under the respective message namespace. * * @return array */ $messenger->getMessages();
要检查是否存在特定命名空间下的消息,请使用has
方法
/** * Check if there are messages under the specified message namespace. * * @param string $namespace * @return boolean */ $messenger->has('error');
消息持有者
当您打算向最终用户显示消息时,请使用消息持有者
$messenger->send('a'); $messenger->send('b', 'success'); echo $messenger->getMessageHolder();
<ul class="paddy-messenger with-messages"> <li class="error">a</li> <li class="success">b</li> </ul>
当没有消息时,getMessageHolder
将生成
<ul class="paddy-messenger no-messages"></ul>
空标签用于与前端脚本互操作。
建议的样式表
.paddy-messenger { display: none; li { display: block; padding: 20px; color: #fff; &.error { background: #E74C3C; } &.notice { background: #F1C40F; } &.important { background: #3498DB; } &.success { background: #27AE60; } } &.with-messages { display: block; } }
缩写
/** * Shorthand method to send message under "error" namespace. * * @param string $message * @return $this */ public function error ($message) { return $this->send($message, 'error'); } /** * Shorthand method to send message under "success" namespace. * * @param string $message * @return $this */ public function success ($message) { return $this->send($message, 'success'); } /** * Shorthand method to send message under "notice" namespace. * * @param string $message * @return $this */ public function notice ($message) { return $this->notice($message, 'notice'); }
准持久性
使用$_SESSION
变量在页面之间传输消息。这意味着在使用Paddy之前必须启动会话。
// Page 1 $messenger->error('foo');
带有“location”头的响应会继续持久化消息数据
// Page 2 header('Location: Page 3');
消息在显示第一页后将被丢弃
// Page 3 var_dump($messenger->has('error'));
array(1) {
[0]=>
bool(true)
}
名称
以编号为NPS.43.9451的鸽子命名,Paddy。
日志记录
实现了PSR-3 LoggerAwareInterface
。