stagem / zfc-flash
不同Flash管理器的ZF适配器
Requires
- php: >=7.0
- slim/flash: ^0.4
This package is not auto-updated.
Last update: 2024-09-15 06:05:09 UTC
README
不同Flash管理器的ZF适配器
有许多Flash消息实现,它们都有不同的接口。此插件提供了一个FlashInterface,允许为任何Flash库编写自定义的Adapter。
默认情况下,此软件包使用slim/flash。
软件包有多个已注册的命名空间,允许分组消息并显示所有消息的美丽HTML标记。
- default
- success
- warning
- error
- info
安装
composer require stagem/zfc-flash
在Expressive中
您应在config/config.php中启用模块Stagem\ZfcFlash\ConfigProvider::class,并在config/pipeline.php中注册中间件
$app->pipe(\Stagem\ZfcFlash\FlashMiddleware::class);
这允许通过$flash = $request->getAttribute('flash');从请求中获取flash对象
用法
在Expressive中
use Psr\Http\Server\RequestHandlerInterface; use Zend\Diactoros\Response\RedirectResponse; function($request, RequestHandlerInterface $handler) { $flash = $request->getAttribute('flash'); $flash->addMessage('Hello World!', 'success'); return new RedirectResponse('/other-middleware'); }
开箱即用,此模块与Zend\View\Helper\FlashMessenger和twitter bootstrap一起工作
将flash-messages模板包含到您的模板中,如<?= $this->partial('widget::flash-messages') ?>
如果您想更改HTML标记或样式,请参阅自定义Flash部分。
自定义Flash
请注意,您应在config/config.php中在Stagem\ZfcFlash\ConfigProvider::class之后启用您的模块
您可以为任何Flash Messages开发自定义适配器,只需实现Stagem\ZfcFlash\FlashInterface并在您的config/module.config.php中的dependencies中注册即可
自定义模板
创建新的.phtml文件,获取消息并实现自定义HTML标记。您可以使用current辅助程序或其他方法来获取Request对象,然后获取flash。
// view/widget/flash-messages.phtml #$flash = $this->current('request')->getAttribute('flash'); $messages = $flash->getAllMessages(); // iterate over array
之后,将您的模板添加到config/module.config.php并在模板中包含它
return [ 'templates' => [ 'paths' => [ 'widget' => [__DIR__ . '/../view/widget'], ], ], ];