notifynder4 简单翻译了一下文档

安装

使用composer安装

composer require fenos/notifynder:^4.0

配置

config/app.php里添加

'providers' => [
    Fenos\Notifynder\NotifynderServiceProvider::class,
],
'aliases' => [
    'Notifynder' => Fenos\Notifynder\Facades\Notifynder::class,
],

然后分别执行一下命令

php artisan vendor:publish --provider="Fenos\Notifynder\NotifynderServiceProvider"

php artisan migrate

设置MODEL

如果你是 laravel5.3版本以上,得使用这个\Fenos\Notifynder\Traits\NotifableLaravel53类,功能都差不多,只是怕命名冲突

namespace App\Models;

use Fenos\Notifynder\Traits\Notifable as NotifableTrait; // for Laravel 5.0+
use Fenos\Notifynder\Traits\NotifableLaravel53 as NotifableTrait; // for Laravel 5.3+
use Illuminate\Database\Eloquent\Model as EloquentModel;

class User extends EloquentModel
{
    use NotifableTrait;
}

设置好以后,就可以用正常的laravel方法去发送关于此model的通知了(不限于 User,其他Model一样可以!)

相关配置

config/notifynder.php里相关配置

  • Models
    • model Model类
    • polymorphic 布尔值,是否多态(还不是很清楚干啥的)
    • notification_model 应该是覆盖 上面的model的,因为有时候不一定就User(我在配置文件里都没看到这个)
  • Parser
    • strict_extra
    • translation.enabled 布尔值,禁止\开启通知文本的翻译
    • translation.domain 翻译文件的域名,可以resources/lang/[language]/[domain].php 这样找到文件
  • Builder
    • additional_fields.required 数组,其中每个字段都附件在通知里
    • additional_fields.fillabl 是一个包含附加填充字段的数组 - 这些字段被添加到数据库中,可以被填充但不是必需的。 在additional_fields.required中的字段将自动添加(谷歌翻译)

使用

创建分类

先解释下分类,比如回复艾特 可能文本内容不一样,那么,此时,可以设置一个分类,查找的时候,可以按照 分类的种类来进行查询

  • name 这是通过字符串找到它而不仅仅是ID的类别的名称/键。 该字段必须是唯一的,我们强制它是小写的,带有下划线和点的字母数字。 这也可以是一个点分隔的键,以使类似于命名空间的类别(谷歌翻译)
  • text 如果您使用通知文本翻译,此字段是可选的。 如果不是,这是此类别中所有通知的模板。 您可以使用像{from.name}这样的占位符 - 这些占位符是通知模型上的点分隔数组键。(谷歌翻译)

示例:

$category = \Fenos\Notifynder\Models\NotificationCategory::create([
    'name' => 'user.follow', // we recommend lowercase and dot seperated names
    'text' => 'Hello {to.name}, {from.name} is now following you and want to let you know "{extra.message}".',
]);

翻译通知文本

return [
    'user' => [
        'follow' => 'Hello {to.name}, {from.name} is now following you and want to let you know "{extra.message}".',
        'like' => '{from.name} has liked your {extra.post_type}.',
    ],
    'admin' => [
        'new_user' => 'A new user has registered in your application - {from.name}<{from.email}>.',
    ],
];

发送通知

单条通知

\Notifynder::category('user.following') // define the category to send
    ->from($sender) // 谁发来的(User 里的ID) 比如帖子回复的人
    ->to($receiver) // 发给谁的(User 里的ID) 比如帖子作者
    ->url('http://notifynder.info') // define the url for the notification
    ->extra(['message' => 'Hey John, I\'m Doe.']) // define additional data
    ->expire(Carbon::tomorrow()) // 设置过期时间(如果没有过期时间,可以去掉这个)
    ->send(); // send the notification

匿名通知

\Notifynder::category('user.following') // define the category to send
    ->anonymous() // force an anonymous notification sender
    ->to($receiver) // define the receiver of the notification
    ->url('http://notifynder.info') // define the url for the notification
    ->extra(['message' => 'Hey John, I\'m Doe.']) // define additional data
    ->expire(Carbon::tomorrow()) // define an expire date for the notification
    ->send(); // send the notification

额外字段

\Notifynder::category('user.following') // define the category to send
    ->anonymous() // force an anonymous notification sender
    ->to($receiver) // define the receiver of the notification
    ->url('http://notifynder.info') // define the url for the notification
    ->extra(['message' => 'Hey John, I\'m Doe.']) // define additional data
    ->extra(['action' => 'invitation'], false) // extend additional data
    ->expire(Carbon::tomorrow()) // define an expire date for the notification
    ->send(); // send the notification

发送给多人


$receivers = [1,2,3,4];//接收人的ID
$sender = 100;//发送者的ID

\Notifynder::loop($receivers, function(\Fenos\Notifynder\Builder\Builder $builder, $receiver) use ($sender) {
    $builder->category('user.following') // define the category to send
        ->from($sender) // define the sender of the notification
        ->to($receiver) // define the receiver of the notification
        ->url('http://notifynder.info') // define the url for the notification
        ->extra(['message' => 'Hey John, I\'m Doe.']) // define additional data
        ->expire(Carbon::tomorrow()); // define an expire date for the notification
})->send(); // send the notification

自定义字段

\Notifynder::category('user.following') // define the category to send
    ->from($sender) // define the sender of the notification
    ->to($receiver) // define the receiver of the notification
    ->setField('customer_id', $customer->getKey()) // set the customer_id field
    ->send(); // send the notification

获取通知

利用user 来获取通知

$user = User::find(1);//谁的通知,获取谁的实例
$notifications = $user->getNotifications(); // all notifactions desc ordered by created_at
$notifications = $user->getNotifications(null, 'asc'); // all notifactions asc ordered by created_at
$notifications = $user->getNotifications(10); // 10 notifactions desc ordered by created_at
$notifications = $user->getNotifications(10, 'asc'); // 10 notifactions asc ordered by created_at
$notifications = $user->getNotificationRelation() // get all notifications in category 2 that are not expired and order by read status
                    ->where('category_id', 2)
                    ->where(function($query) {
                        $query
                            ->whereNull('expires_at')
                            ->orWhere('expires_at', '>=', Carbon::now());
                    })
                    ->orderBy('read', 'asc')
                    ->get();

利用\Fenos\Notifynder\Models\Notification来获取通知

用法跟 laravel里 Model常规用法一样

处理通知

Controller

public function getIndex()
{
    $user = \Auth::user();
    $notifications = $user->getNotifications(25);

    return view('notification.index')->with(compact($user, $notifications));
}

public function getRead(Notification $notification)
{
    $notification->read();
    if(empty($notification->url)) {
        return redirect()->back();
    }
    return redirect()->to($notification->url);
}

View

<ul id="notifications-list">
    @foreach($notifications as $notification)
        <li class="@if(!$notification->read) active @endif">
            <a href="{{ route('notification.read', $notification->getKey()) }}">
                <span class="text-content">
                    {{ $notification->text }}
                </span>
                <span class="date">
                    {{ $notification->created_at->diffForHumans() }}
                </span>
            </a>
        </li>
    @endforeach
</ul>

如果要 将 某条通知 标记为已读,上面 控制器里的用法试了,感脚不对....

代码

$notification = Notification::find(1);//哪条通知
 $notification->read();//返回true或者false
说实话,这作者... 算了,不评价了... 哈哈

欢迎转载,但请附上原文地址哦,尊重原创,谢谢大家 本文地址: http://www.iphpt.com/detail/90/

当你能力不能满足你的野心的时候,你就该沉下心来学习