smartsoftware / l4-notifier
v0.1.4
2015-10-01 15:58 UTC
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
README
特性
- 附件对象
- 带有模板的类型
- 在模板中对对象的属性和关系进行访问
- 通过类型进行回调,以定义必须接收通知的用户
安装与配置
使用 Composer
$ composer require smartsoftware/l4-notifier
发布配置文件
$ php artisan view:publish smartsoftware/l4-notifier
运行迁移
$ php artisan migrate --package='smartsoftware/l4-notifier'
编辑配置文件
#!php
<?php
return array(
/**
* User Model
*/
'user_model' => 'User',
/**
* get_users id's by notification type for batch notifications
*/
'get_users' => [
/**
* @param int $type Notification type id
* @param mixed $obj Attached Object
* @param int $sender User id of notification
*/
'1' => function($type, $obj, $sender) {
return [11,12]; // User 11 and 12 will recive notifications of type 1
},
'2' => function($type, $obj, $sender) {
$users = User::whereHas('roles', function($q) {
$q->where('name', 'someRole');
});
return $users; // User with role someRole will recive notifications of type 2
}
]
);
将特质添加到用户模型中
#!php
<?php
use Smartsoftware\L4Notifier\NotifiedTrait;
class User extends Eloquent {
use NotifiedTrait;
...
...
}
创建通知类型
要创建通知,我们使用 l4notifier:createtype 命令
Usage:
l4notifier:createtype subject [body] [url]
Arguments:
subject Notification Subject.
body Notification Body.
url Notification URL.
php artisan l4notifier:createtype "#{obj.id} Ticket closed" "The ticket #{obj.id} was closed by {from.name}" "http://tickets.com/{obj.id}"
可用于模板的对象有
obj: the attached object (eloquent)
from: sender user
to: receiver user
You could access to obj propierties like this
{from.role.id}
用法
发送通知
向单个用户发送通知
#!php
<?php
$ticket = Ticket::find(10);
$user->newNotification()
->withType(1)
->regarding($ticket)
->from($sender_user)
->deliver();
向多个用户发送通知
#!php
<?php
use Smartsoftware\L4Notifier\BatchNotification;
$ticket = Ticket::find(10);
$users = [1,4,5,110,22];
BatchNotification::notify($users, 1, $obj, $sender_user->id);
检索通知
获取当前用户的通知
#!php
<?php
$user = Auth::user();
$notifications = $user->notifications()
->unread()
->with('sender')
->orderBy('sent_at','desc');