jcergolj/in-app-notifications-for-laravel

用于设置和显示 Laravel 内置通知的简单包

v0.5 2024-06-04 08:45 UTC

This package is auto-updated.

Last update: 2024-09-04 09:16:00 UTC


README

Package for displaying in app notifications for Laravel

关键特性

  • 通知在5秒后自动关闭
  • 您可以更改超时时间
  • 默认使用 tailwind css 进行样式设计
  • 您可以修改组件并使用不同的框架或完全不使用框架

安装

要求

PHP >=8.2

安装包

composer require jcergolj/in-app-notifications-for-laravel

发布配置文件

如果您想修改组件视图

php artisan vendor:publish --provider="Jcergolj\InAppNotifications\InAppNotificationsServiceProvider"

使用方法

将组件添加到 layouts/app.blade.php 文件中

    <x-in-app-notifications::notification />

    @yield('scripts')

在控制器中设置通知

<?php

namespace App\Http\Controllers;

use Jcergolj\InAppNotifications\Facades\InAppNotification;

class UserController extends Controller
{
    public function store(Request $request)
    {
        InAppNotification::success('User was successfully created.');

        return view('mailboxes.index');
    }
}

可用方法

<?php

InAppNotification::success($message);

InAppNotification::error($message);

InAppNotification::warning($message);

InAppNotification::info($message);

更改默认超时时间

在 register 的 AppServiceProvider.php 文件中

<?php

public function register(): void
{
    $this->app->bind('in-app-notification', function () {
        return new Jcergolj\InAppNotifications\InAppNotification(10000); // 10 seconds
    });
}

它使用 Illuminate\Support\Traits\Macroable;

如果您愿意,可以扩展 InAppNotitication 类,利用 Macroable 特性。

// AppServiceProvider.php

InAppNotitication::macro('customMethod', function () {

});

// e.g. inside controller

InAppNotification::customMethod();

测试

包附带一些实用的断言

// add this for adding testable macro
InAppNotification::fake();

InAppNotification::assertSet()
InAppNotification::assertSet('Assert against this text.');

InAppNotification::assertSuccess()
InAppNotification::assertSuccess('Success');

InAppNotification::assertError();
InAppNotification::assertError('Error');

InAppNotification::assertInfo();
InAppNotification::assertInfo('Info');

InAppNotification::assertWarning();
InAppNotification::assertWarning('Warning');

InAppNotification::assertTimeout(10000);