jbruni / larnotify
Laravel 4的通知服务
Requires
- php: >=5.3.0
- illuminate/support: 4.0.x
This package is not auto-updated.
Last update: 2024-09-23 14:44:15 UTC
README
使用composer,在您的laravel项目根目录下
composer require jbruni/larnotify
然后,将服务提供者添加到app/config/app.php
文件中的providers
数组
'Jbruni\Larnotify\NotificationServiceProvider',
建议您在同一个文件中,将别名添加到aliases
数组
'Larnotify' => 'Jbruni\Larnotify\Larnotify',
用法
基础
基本上,您可以通过在任何应用程序中的add
方法添加通知
Larnotify::add('Settings saved.');
您可以在任何视图中生成输出
{{ $messages }}
add
方法接受一个可选的第一个参数,可以指定消息类型
Larnotify::add('warning', 'You have only 1 credit remaining.');
Larnotify::add('error', 'Failed to get the selected item.');
并渲染特定的消息类型
{{ $messages['warning'] }}
您还可以对消息进行命名空间。例如,在Facebook中,您有“好友请求”、“消息”和“通知”- 三个不同的通知块...
Larnotify::add('requests.unread', 'John wants to be your friend.');
Larnotify::add('requests.read', 'Mary wants to be your friend.');
Larnotify::add('messages.new', 'Hi! How are you?');
Larnotify::add('notifications.liked', 'Pete liked your post.');
在输出时...
{{ $messages['requests.ALL'] }}
{{ $messages['messages.new'] }}
实际上,每条消息都有一个命名空间和一个消息类型,即使没有指定。
默认命名空间是“默认”且默认消息类型是“msg”。
因此,在前两个示例(上面)中,消息存储在“default.msg”和“default.warning”命名空间/类型。
输出格式化
有两种特殊的消息类型:“sprintf”和“view”
Larnotify::add('sprintf:Your name is %s.', 'John Doe');
Larnotify::add('view:paused_service', array('date' => '10/10/2013'));
第一个示例使用“sprintf”命令渲染。第二个参数需要是一个包含剩余“sprintf”参数的数组或一个单个字符串。
“view”类型允许您指定一个视图名称及其参数。
这两种消息类型都接受一个命名空间。
Larnotify::add('info.sprintf:Hello, %s! You have earned %s points.', array('Mary Jane', '100'));
上述通知将存储在“info.sprintf”命名空间/类型,并使用以下方式渲染
sprintf('Hello, %s! You have earned %s points.', 'Mary Jane', '100');
现在,使用view
的示例
Larnotify::add('info.view:user.balance', array('amount' => '108.90');
这将可用在“info.view”,并使用以下方式渲染
View::make('user.balance', array('amount' => '108.90'));
如果在一个请求中同时调用,由于它们属于同一个命名空间,所以两者都将一起渲染
{{ $messages['info'] }}
如果您想选择其中一种类型
{{ $messages['info.sprintf'] }}
{{ $messages['info.view'] }}
分组模板
正如我们所看到的,这些“view”和“sprintf”模板是为单个通知指定的。
可以指定一个模板,该模板将渲染所有分配给它的通知。
示例
Larnotify::add('user.info', 'Account successfully created.');
Larnotify::add('user.info', 'You have earned 200 bonus points.');
通过配置,可以在配置文件或运行时进行配置,将一个视图分配给渲染它们
// config
'views' => array(
'user.info' => 'infowidget'
);
//runtime
Larnotify::setView('user.info', 'infowidget');
(注意:如果存在“user.info”视图,它将自动使用。不需要配置或“setView”)
对于“infowidget”视图,将有一个包含相应通知的数组在$notifications
变量中。
您可以遍历它们,并根据需要渲染。
结果将在以下位置可用
{{ $messages['user.info] }}
请注意,没有任何阻止您用数组而不是字符串作为消息发送。这允许您在视图中进一步处理通知
Larnotify::add('commits.latest', array('repository_name' => 'Larnotify', 'hashes' => array('af12ca72', 'b7m2o018', 'abcdef78')));
Larnotify::add(array('author' => 'Taylor', 'tweet' => 'Well done!'));
JSON
与其在模板中渲染HTML并将其包含在内,或者发送AJAX响应到DOM中,您可能已经处理了一个强大的前端应用程序,使用Angular.JS或类似的应用程序,并且您只想有原始数据,因为您将通过客户端JavaScript执行所有的DOM魔法...
在这种情况下,您可以将消息或模板数据以JSON格式直接获得
Larnotify::getJson('requests.all');
或在模板中
{{ $messages->getJson('user.info'); }}
现在,您开始认为并感觉Larnotify可以用于远超通知...不是吗?
配置
以下是当前配置文件:
<?php
return array(
/**
* string View templates (add an entry for each message type)
*
* NOTE: "ALL", "view" and "sprintf" are reserved message type names
*/
'views' => array(
'default.msg' => '',
),
/**
* string Global view variable name (Larnotify object)
*/
'view_share' => 'messages',
/**
* string Notifications variable name (Array of messages)
*/
'msg_variable' => 'notifications',
/**
* string Use this sprintf template to render messages if none is provided
*/
'default_template' => '<p class="%2$s %3$s">%1$s</p>',
/**
* string String to be included between each block of rendered output
*
* NOTE: "\n" is recommended
*/
'block_splitter' => '',
);
如果没有可用的模板,则将使用default_template
作为sprintf参数,接收消息、命名空间和消息类型。
因此,默认情况下,Larnotify::add('You won!');
渲染为:
<p class="default msg">You won!</p>
您可能希望直接使用CSS,而不是其他任何东西。
所有视图中可用的全局变量$messages
用于访问Larnotify,但您可以通过view_share
配置选项进行更改。
同样,消息将以$notifications
的形式可用于渲染分组模板,但您可以通过msg_variable
配置选项进行更改。
最后,在渲染时包含block_splitter
字符串,在Larnotify渲染的每个块之间。我始终使用"\n",因此在查看生成的HTML源时,每个通知都将在新的一行开始。但这可能不是预期的行为,因此此“魔法”默认是关闭的。
我们已经在“分组模板”部分中介绍了views
配置选项。它是一个数组,键是带有或不带有前缀的的消息类型,值是视图模板名称。