MichaelJennings / feed
laravel 5+ 简单活动流
Requires
- illuminate/database: 5.*
Requires (Dev)
- laravel/laravel: 5.*
- mockery/mockery: 0.9.*
- phpunit/dbunit: 2.0.*
- phpunit/phpunit: 4.8.*
- satooshi/php-coveralls: 1.0.*
README
注意:此包不再维护 Laravel 5.3+,因为 Laravel 现在提供内置的 通知。
laravel 5+ 基本通知流。
以下是该包所有基本方法的示例代码。
$user = User::find(1); $team = Team::find(1); // Push notification to a user $feed->push('This is a new notification', $user); // Push notification to a user, and a team of users $feed->push('This is a new notification', [$user, $team]); // Push notification to a user with multiple parameters $feed->push([ 'icon' => 'icon-alert', 'title' => 'Something Broke!', 'body' => 'Something super important broke' ], $user); // Get all of the notifications for a user $notifications = $feed->pull($user); // Get 10 notifications for the user $notifications = $feed->limit(10)->pull($user); // Mark a notification as read $feed->markAsRead($notification);
导航
安装
此包至少需要 laravel 5。
要通过 composer 安装,请将包包含在您的 composer.json 文件中。
"michaeljennings/feed": "0.2.*"
运行 composer install 或 composer update 下载依赖项,或者您可以直接运行 composer require michaeljennings/feed。
安装完成后,将服务提供者添加到 config/app.php 文件中的 providers 数组中。
'providers' => [ Michaeljennings\Feed\FeedServiceProvider::class ];
要发布迁移和配置文件,请运行 php artisan vendor:publish。
配置
该包包含一个默认迁移,用于创建包的数据库结构。默认情况下,此表允许通知有正文和图标。如果需要更多数据,例如标题,我们建议在默认迁移中添加列。
该包包含 feed.php 配置文件。这允许您自定义与包一起使用的数据库驱动程序。目前仅支持 eloquent,但我们正在开发一个 Laravel 数据库驱动程序。
更改通知模型
有时您可能需要向通知模型添加更多方法或属性,例如,您可能希望向通知模型添加额外的关联。
这可以通过如下所示更改配置文件中的通知模型来完成。
'drivers' => [ 'eloquent' => [ 'model' => 'Path\To\Notification', // Update this to your notification model. ], ]
默认通知模型实现了该包所需的几个接口。如果您需要修改模型,我建议扩展默认模型;否则,请确保实现这些接口。
// Example of extending the model class Notification extends \Michaeljennings\Feed\Store\Eloquent\Notification { public function foo() { // } } // Example of just implementing the interfaces class Notification implements \Michaeljennings\Feed\Contracts\Notification, \Michaeljennings\Feed\Contracts\Store { public function bar() { // } }
添加驱动程序
您可能需要另一个驱动程序,即您使用的数据存储库不受 Laravel 支持。如果是这种情况,您可以按照以下所示简单地将驱动程序添加到系统中。
// Here we grab the driver manager and then extend it to a new 'foo' driver. // Ideally this would be done within a service provider. app('feed.manager')->extend('foo', function($app) { return new Foo(); }); // The Foo driver needs to implement the Store interface so it has all of // the necessary methods. class Foo implements Michaeljennings\Feed\Contracts\Store { // } // The in the config file set the driver to our new foo driver. return [ 'driver' => 'foo' ]
有关添加驱动程序的更多信息,请参阅 Laravel 扩展框架的文档。
使用流
安装完成后,您可以通过多种方式访问流。
首先,您可以通过 IOC 容器通过推送或拉取流接口进行依赖注入。这两个接口将返回相同的实例,这只是为了使您的代码更易读。
public function __construct( Michaeljennings\Feed\Contracts\PullFeed $pullFeed, Michaeljennings\Feed\Contracts\PushFeed $pushFeed ) { $this->pullFeed = $pullFeed; $this->pushFeed = $pushFeed; }
或者,有一个 feed 辅助方法。
$feed = feed();
或者,如果您想使用外观,您可以在 config/app.php 文件中的 aliases 数组中注册它。
'aliases' => [ 'Feed' => Michaeljennings\Feed\Facades\Feed::class ]
设置可通知模型
要设置可通知模型,只需实现可通知接口,然后在您的模型中使用可通知特质。
这将设置所需的关系。
use Michaeljennings\Feed\Contracts\Notifiable as NotifiableContract; use Michaeljennings\Feed\Notifications\Notifiable; class User extends Model implements NotifiableContract { use Notifiable; }
可通知组
还可以设置通知模型组,这在有一支用户团队时会很有用。这可以使我们向该组的所有成员发送通知。
要设置通知组,您需要在组模型上实现通知组接口。这将添加一个名为 getGroup 的方法,您需要返回您希望通知的成员。
在下面的示例中,我们有一个实现了组接口的团队模型。它有一个 users 关联,返回属于团队的所有用户。然后在 getGroup 方法中,我们只需返回用户。
use Michaeljennings\Feed\Contracts\NotifiableGroup; class Team extends Model implements NotifiableGroup { public function users() { return $this->hasMany('App\User'); } public function getGroup() { return $this->users; } }
可用方法
以下是所有当前可用的通知方法。如果您想添加任何内容,请随时创建问题或拉取请求。
推送
推送方法允许您向通知模型、多个通知模型或通知组推送通知。
当推送到一个通知组时,每个组员都会收到自己的通知,不会为所有成员共享一个通知。
$feed->push('My awesome notification', $user); $feed->push('My awesome notification', [$user, $team]); $feed->push([ 'title' => 'New Notification', 'body' => 'My awesome notification' ], $user);
当推送通知时,将触发一个 NotificationAdded 事件。
然后您可以监听此事件,然后广播通知,发送电子邮件等。
您只需在事件服务提供程序中注册监听器。
protected $listen = [ 'Michaeljennings\Feed\Events\NotificationAdded' => [ 'App\Listeners\BroadcastNotification', 'App\Listeners\EmailNotification', ], ];
拉取
拉取方法获取您传递给它的通知模型的所有未读通知。
$feed->pull($user);
拉取已读
要获取成员的所有已读通知,请使用 pullRead 方法。
$feed->pullRead($user);
限制结果
要限制拉取时返回的通知数量,请链式调用 limit 方法。
$feed->limit(10)->pull($user); $feed->limit(10)->pullRead($user);
偏移结果
要拉取时偏移结果,请链式调用 offset 方法。这对于无限滚动很有用。
$feed->offset(10)->pull($user); $feed->offset(10)->pullRead($user);
分页结果
如果您想分页结果并让 Laravel 处理限制和偏移,请链式调用 paginate 方法。
$feed->paginate(10)->pull($user); $feed->paginate(10)->pullRead($user);
过滤结果
有时您可能需要在通知结果上运行额外的查询,为此请链式调用 filter 方法。
filter 方法需要一个闭包,该闭包传递一个查询构建器实例。在下面的示例中,我们只获取具有警报图标的条目。
$feed->filter(function($query) { $query->where('icon', 'icon-alert'); })->pull($user); $feed->filter(function($query) { $query->where('icon', 'icon-alert'); })->pullRead($user);
获取最新结果
要按最新添加的通知排序结果,请链式调用 latest 方法。默认情况下,通知按最新添加排序。
$feed->latest()->pull($user); $feed->latest()->pullRead($user);
获取最旧结果
要按最早添加的通知排序结果,请链式调用 oldest 方法。默认情况下,通知按最新添加排序。
$feed->oldest()->pull($user); $feed->oldest()->pullRead($user);
综合运用
所有这些方法都可以链式调用,这应该允许您以所需的方式获取通知。
// Limit and offset the results $feed->limit(10)->offset(10)->pull($user); // Get all of the oldest read notifications, that have an alert icon. $feed->filter(function($query) { $query->where('icon', 'icon-alert'); })->oldest()->pullRead($user);
标记通知为已读
要标记通知为已读,您可以使用 markAsRead 方法,或者如果您更喜欢,它还别名到 read。
$feed->markAsRead($notification); $feed->read($notification);
当通知标记为已读时,将触发一个 NotificationRead 事件。
然后您可以监听此事件,然后广播等。
protected $listen = [ 'Michaeljennings\Feed\Events\NotificationRead' => [ 'App\Listeners\BroadcastReadNotification', ], ];
标记通知为未读
要标记通知为未读,您可以使用 markAsUnread 方法,或者如果您更喜欢,它还别名到 unread。
$feed->markAsUnread($notification); $feed->unread($notification);
当通知标记为未读时,将触发一个 NotificationUnread 事件。
然后您可以监听此事件,然后广播等。
protected $listen = [ 'Michaeljennings\Feed\Events\NotificationUnread' => [ 'App\Listeners\BroadcastUnreadNotification', ], ];