coreplex / notifier
一个PHP类,用于将通知简单地闪现到会话中,然后使用JavaScript通知器显示它们。
Requires
- php: >=5.4.0
- coreplex/core: ~0.1
Requires (Dev)
- phpunit/phpunit: 4.8.*
- satooshi/php-coveralls: 1.0.0
README
一个PHP包,用于简单地将通知闪现出来,然后使用JavaScript通知器渲染它们。此包还自带Laravel支持。
安装
此包需要PHP 5.4+,并包含Laravel 5服务提供者和外观。
我们建议通过composer安装此包。您可以在命令行中调用composer require coreplex/notifier
,或者将以下内容添加到您的composer.json
中,然后运行composer install
或composer update
来下载此包。
"coreplex/notifier": "~0.2"
Laravel 5集成
要使用Laravel 5与包一起使用,我们首先需要将通知器和核心服务提供者添加到app/config/app.php
中的提供者数组中。
'providers' => array( Coreplex\Core\CoreServiceProvider::class, Coreplex\Notifier\NotifierServiceProvider::class, );
如果您想使用外观,请将以下内容添加到您的app/config/app.php
中的别名数组中。
'aliases' => array( Notifier' => Coreplex\Notifier\Facades\Notifier::class, );
最后,一旦您添加了服务提供者,请运行php artisan vendor:publish
以发布配置文件。
使用
通知器由几个组件组成;一个模板解析器和coreplex核心会话类。
要创建模板解析器,只需创建一个新的Parser类实例。此类没有依赖项,因此只需创建对象即可。
$parser = new Coreplex\Notifier\Parser();
有关如何使用coreplex核心会话类的说明,请参阅文档。
一旦您有了模板解析器和会话,就可以创建通知器。通知器还需要作为第三个参数的包配置。
$notifier = new Coreplex\Notifier\Notifier($parser, $session, $config);
与Laravel一起使用
一旦您设置了包以访问通知器,您可以使用外观或通过接口注入类。
Notifier::render(); public function __construct(\Coreplex\Notifier\Contracts\Notifier $notifier) { $this->notifier = $notifier; }
添加通知
要添加通知,您可以通过调用notify
方法并传递通知级别,或者调用通知级别作为方法。您在通知器配置中指定通知级别,您可以在下面的设置通知器部分中了解更多信息。
$notifier->notify('success'); $notifier->success();
要将数据传递给通知,请传递键值对数组。
$notifier->notify('success', [ 'title' => 'Success', ]); $notifier->success([ 'title' => 'Success', ]);
您可以在每个请求中添加多个通知,并且它们都会被渲染。
$notifier->success(['title' => 'Foo']); $notifier->error(['title' => 'Bar']);
渲染通知
要渲染通知,请调用render
方法。
<?php echo $notifier->render(); ?>
设置通知器
要添加通知器,请将其添加到notifiers
数组中的notifier.php
配置文件中。默认情况下,此包为优秀的alertify通知器提供了配置。
'notifiers' => [ 'alertify' => [ 'template' => 'alertify.{{ level }}("<strong>{{ title }}</strong><br>{{ message }}");', 'css' => [], 'scripts' => [], 'levels' => [ 'info' => 'log', 'success' => 'success', 'error' => 'error', ], ], ],
通过添加由模板和通知级别组成的数组来注册通知器,可选地还有任何CSS文件和JavaScript脚本。
设置模板
模板用于渲染通知。当您调用render
方法时,它将遍历所有通知并将它们渲染到该模板。
模板始终传递通知级别,然后还传递动态数据数组。要访问数据,请将键包裹在双大括号中,如下所示
'template' => 'notifier.{{ level }}("Hello World!");'
如果使用此模板调用成功通知,它将渲染为以下字符串。
<script>notifier.success("Hello World!");</script>
非常可能您不希望硬编码通知的主体,因此要在模板中访问这些动态数据,可以使用以下模板。
'template' => 'notifier.{{ level }}("{{ body }}");'
有时您可能只想在数据通过时渲染。例如,在以下模板中,只有传递了图标时才会渲染图标。
'template' => 'notifier.{{ level }}("[if:icon] {{ icon }} [endif] {{ body }}");
设置通知级别
每个通知器可能对其级别有不同的命名。为了增加一致性,默认情况下,该包需要info、success和error级别的通知。在级别数组中,您可以指定通知器用于这些级别的名称。
例如,对于alertify包,它自带了log、success和error通知级别
。因此,级别数组看起来如下所示
'levels' => [ 'info' => 'log', 'success' => 'success', 'error' => 'error', ],
然而,您可能希望访问比这更多的级别,因此要注册自定义级别,只需将它们添加到数组中。
'levels' => [ 'info' => 'log', 'success' => 'success', 'error' => 'error', 'custom' => 'fooBar', ],
然后,您可以使用notify
方法或通过在通知器实例上调用名称来调用此级别。
$notifier->notify('custom'); $notifier->custom();
设置脚本和样式
如果您正在使用多个通知器,那么您可能不希望在每次请求时都加载所有样式或脚本,而只在该通知器被使用时加载。为此,您可以在css
数组中设置样式路径,在scripts
数组中设置js文件路径。
'css' => [ 'path/to/styles.css' ], 'scripts' => [ 'path/to/scripts.js' ],
然后,使用styles
方法渲染样式,使用scripts
方法渲染脚本。
<head> <?php echo $notifier->styles(); ?> </head> <body> <?php echo $notifier->scripts(); ?> </body>