tashkar18/notification

此包最新版本(0.3)没有可用的许可证信息。

Laravel 4.2 的简单通知系统

0.3 2016-04-03 19:33 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:20:34 UTC


README

快速开始

所需设置

使用 composer 命令要求 tashkar18\notification

$ composer require tashkar18/notification ~0.2

在您的 config/app.php 文件中,

Tashkar18\Notification\NotificationServiceProvider 添加到 providers 数组的末尾

  'providers' => array(
    'Illuminate\Foundation\Providers\ArtisanServiceProvider',
    'Illuminate\Auth\AuthServiceProvider',
    ...
    'Tashkar18\Notification\NotificationServiceProvider',
  ),

现在生成配置文件

$ php artisan config:publish tashkar18/notification

然后迁移 notifications

$ php artisan migrate --package="tashkar18/notification"

设置

在您的任何一个 Eloquent 模型中,实现 NotificationInterface 并使用 NotificationTrait

use Tashkar18\Notification\NotificationInterface;
use Tashkar18\Notification\NotificationTrait;

class Comment extends Eloquent implements NotificationInterface
{
    use NotificationTrait;

然后在您的模型中实现这些方法。

/**
 * This determines the recipient id of the event.
 * For example, if a user comments on a post, the recipient of the
 * notification would be the post's author.
 * @return {int}
 */
public function getNotificationRecipient()
{
    return $this->post->user_id;
}

/**
 * This determines the sender id of the event.
 * For example, if a user comments on a post, the sender of the
 * notification would be the comment's author. (This will typically
 * be user_id, but you might also use a different attribute for the user_id like author_id);
 * @return {int}
 */
public function getNotificationSender()
{
    return $this->user_id;
}

您可以将 NoterTrait 添加到您的 User 模型中,以设置用户 hasMany 关系

use Tashkar18\Notification\NoterTrait;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait, NoterTrait;

然后您将能够访问用户通知

return $user->notifications;

配置

创建您的 Notification-特定视图文件夹,并将 packages/tashkar18/notification/config.php 进行调整以匹配

  return array(
      'view' => 'notifications'
  );

为您的 Eloquent 模型创建 ViewPresenter

您的视图呈现器文件只是一个 view 文件,它将以人类可读文本呈现您的通知。

例如,一个 Comment 模型将在 views\notifications\comment.blade.php 中有一个视图文件。 comment 对象将自动传递到该视图中,可以使用变量 $comment 访问。

// views/notifications/comment.blade.php
{{{ ucfirst($comment->user->username) }}} commented on your post.

测试(即将推出)