williamcruzme/laravel-notification-settings

一个允许您在发送前检查通知设置的 Laravel 扩展包。

v1.0.1 2021-05-22 22:43 UTC

This package is auto-updated.

Last update: 2024-09-22 11:21:07 UTC


README

Laravel Laravel GitHub


laravel-notification-settings 是一个允许您在发送前检查通知设置的 Laravel 扩展包。

💿 安装

composer require williamcruzme/laravel-notification-settings

🏁 入门指南

1. 添加特质

在用户模型中添加 Notifiable 特质。此特质支持自定义守卫

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Millions\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;
}

2. 运行迁移

php artisan migrate

有时您可能需要自定义迁移。使用 vendor:publish 命令,您可以导出迁移

php artisan vendor:publish --tag=migrations

3. 创建种子

添加所有需要设置的通知。未添加的通知将不会被验证即发送

php artisan make:seeder NotificationTypesTableSeeder
/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
    DB::table('notification_types')->insert([
        'name' => 'App\\Notifications\\Welcome',
        'display_text' => 'Welcome message',
        'status' => true,
    ]);
}

4. 添加路由

使用 Notification 门面导入管理设置的路径

Notification::routes();

🚀 使用说明

使用可通知特质

此特质包含一个可能用于发送通知的方法:notify。该方法检查用户是否希望接收通知,并期望接收一个通知实例

use App\Notifications\InvoicePaid;

$user->notify(new InvoicePaid($invoice));

使用通知门面

或者,您可以通过 Notification 门面发送通知。这在需要向多个可通知实体(如用户集合)发送通知时非常有用。要使用门面发送通知,请将所有可通知实体和通知实例传递给 send 方法

use Millions\Notifications\Facades\Notification;

Notification::send($users, new InvoicePaid($invoice));

🌐 路由

获取通知

标记为已读

删除通知

获取通知设置

更新通知设置

Body 参数

{
    "status": true, // false
}

🎨 自定义

首先,创建自己的 NotificationSettingController 控制器,并添加 ManageNotificationSettings 特质。

其次,修改 Notification 门面路由的命名空间

Notification::routesForSettings('App\Http\Controllers');

自定义请求验证

NotificationSettingController 中的 rulesvalidationErrorMessages 方法允许您覆盖默认的请求验证

<?php

namespace App\Http\Controllers;

use Millions\Notifications\ManageNotificationSettings;

class NotificationSettingController extends Controller {

    use ManageNotificationSettings;
    
    /**
     * Get the notification settings validation rules.
     *
     * @return array
     */
    protected function rules()
    {
        return [
            'status' => ['required', 'boolean'],
        ];
    }

    /**
     * Get the notification settings validation error messages.
     *
     * @return array
     */
    protected function validationErrorMessages()
    {
        return [];
    }
}

自定义响应

NotificationSettingController 中的 sendResponse 方法允许您覆盖默认的响应

<?php

namespace App\Http\Controllers;

use Millions\Notifications\ManageNotificationSettings;

class NotificationSettingController extends Controller {

    use ManageNotificationSettings;
    
    /**
     * Get the response for a successful listing notification settings.
     *
     * @param  array  $response
     * @return \Illuminate\Http\JsonResponse
     */
    protected function sendResponse($response)
    {
        return response()->json($response);
    }
}

自定义守卫

NotificationSettingController 中的 guard 方法允许您覆盖默认的守卫

<?php

namespace App\Http\Controllers;

use Millions\Notifications\ManageNotificationSettings;

class NotificationSettingController extends Controller {

    use ManageNotificationSettings;
    
    /**
     * Get the guard to be used during notifications management.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return auth('admin')->guard();
    }
}

🚸 贡献

欢迎您为此项目做出贡献,但在您这样做之前,请确保您已阅读贡献指南

🔒 许可证

MIT