jeffersonsouza / zeal-messages
由 Tim Fountain<tim@tfountain.co.uk> 开发的一个简单的 helper/controller 插件,用于显示应用程序消息。
dev-master
2014-03-18 16:31 UTC
Requires
- php: >=5.3.3
This package is not auto-updated.
Last update: 2024-09-24 06:31:38 UTC
README
本模块提供了一个简单的方法来显示消息。它被设计为 ZF 的 flash messenger 的替代品,除了 flash messenger 的功能外,还包括设置不同类型消息(例如,'info' 消息、'error' 消息)的功能,以及立即向用户显示消息(而不是像 flash messenger 那样在下一次请求时显示)。
安装
最佳安装方式是使用 Composer (https://composer.php.ac.cn)。将其添加到您的 composer.json
"require": {
"zealproject/zeal-messages": "dev-master"
}
然后运行 composer install 进行安装。
您还需要编辑您的 application.config.php 文件,以将 ZealMessages 添加到模块数组中。
用法
要从控制器操作添加消息,立即向用户显示消息
$this->messages()->info('Your message here');
或错误消息
$this->messages()->error('Your error message here');
也支持 'success' 消息。
要添加 flash 消息(这些消息将被添加到会话中,并在后续请求中显示给用户),用法如下
$this->messages()->flashInfo('Your message here');
或
$this->messages()->flashError('Your error message here');
等等。
因此,处理表单数据的控制器操作可能看起来像
public function editAction() { [load data and setup $form] if ($this->getRequest()->isPost()) { $form->setData($this->getRequest()->getPost()); if ($form->isValid()) { $data = $form->getData(); if ([code to save $data]) { $this->messages()->flashInfo('Your details were successfully saved'); $this->redirect()->toRoute([some route]); } else { $this->messages()->error('An error occurred saving your details'); } } } return new ViewModel(array( 'form' => $form )); }
渲染消息
模块包括一个用于渲染消息的视图助手。您通常会将其添加到布局中,在视图内容之上
<?=$this->messages()?>
只有当有要显示的消息时,它才会输出 HTML。
消息将以无序列表的形式输出(每个类型一个列表),位于具有 id messages 的 div 中,以便于 CSS 样式化。每个列表的消息类型作为类名,例如
<div id="messages">
<ul class="info">
<li>Your details were successfully saved</li>
</ul>
</div>