rickselby / laravel-edvinaskrucas-notification
用于帮助管理 Laravel 中的闪存/即时通知/消息的包。
Requires
- php: ^8.1
- illuminate/session: 10.*|11.*
- illuminate/support: 10.*|11.*
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^10.0
README
Laravel 的简单通知管理包。
- 通知容器
- 通知集合
- 通知消息
- 通知格式
- 闪存/即时通知
- 方法链式调用
- 消息定位
安装
只需通过 composer.json 在您的 Laravel 安装中添加新包。
"rickselby/laravel-edvinaskrucas-notification": "6.*"
然后运行 composer update
版本矩阵
在 Laravel 中使用它
将以下行添加到 app/config/app.php
服务提供者数组
\Krucas\Notification\NotificationServiceProvider::class,
内核中间件数组(必须在 'Illuminate\Session\Middleware\StartSession' 中间件之后放置
)
\Krucas\Notification\Middleware\NotificationMiddleware::class,
现在您可以使用 Laravel4 了。
发布配置文件
如果您想编辑默认配置文件,只需将其发布到您的应用程序文件夹中。
php artisan vendor:publish --provider="\Krucas\Notification\NotificationServiceProvider" --tag="config"
用法
默认用法
将消息添加到默认容器。
\Krucas\Notification\Facades\Notification::success('Success message'); \Krucas\Notification\Facades\Notification::error('Error message'); \Krucas\Notification\Facades\Notification::info('Info message'); \Krucas\Notification\Facades\Notification::warning('Warning message');
容器
容器允许您为不同的占位符设置不同的容器。
您可以通过传递闭包来修改容器,只需使用以下语法即可
\Krucas\Notification\Facades\Notification::container('myContainer', function($container) { $container->info('Test info message'); $container->error('Error'); });
您还可以像这样访问容器
\Krucas\Notification\Facades\Notification::container('myContainer')->info('Info message');
方法链式调用
\Krucas\Notification\Facades\Notification::container('myContainer')->info('Info message')->error('Error message');
如果您想使用默认容器,只需将容器名称作为 null
即可。名称将从配置文件中获取。
\Krucas\Notification\Facades\Notification::container()->info('Info message');
即时通知(在同一请求中显示)
库不仅支持闪存消息,如果您想在同一请求中显示通知,只需使用
\Krucas\Notification\Facades\Notification::successInstant('Instant success message');
自定义单条消息格式
想要为单条消息自定义格式?没问题
\Krucas\Notification\Facades\Notification::success('Success message', 'Custom format :message');
您还可以传递第二个参数(格式)来格式化消息,但您可以像上面那样单独格式化消息。
添加对象消息
您可以添加对象消息
\Krucas\Notification\Facades\Notification::success( \Krucas\Notification\Facades\Notification::message('Sample text') );
当添加对象消息时,您可以向消息添加额外的参数
\Krucas\Notification\Facades\Notification::success( \Krucas\Notification\Facades\Notification::message('Sample text')->format(':message') );
添加闭包消息
您可以使用闭包添加消息
\Krucas\Notification\Facades\Notification::success(function (Message $message) { $message->setMessage('Sample text')->setPosition(1); });
访问容器中的第一条通知
您可以访问并仅显示容器中的第一条通知
{!! \Krucas\Notification\Facades\Notification::container('myContainer')->get('success')->first() !!}
从所有类型中访问第一条通知
{!! \Krucas\Notification\Facades\Notification::container('myContainer')->all()->first() !!}
显示通知
要显示默认容器中的所有通知,您只需在视图文件中添加一行即可
{!! \Krucas\Notification\Facades\Notification::showAll() !!}
当使用 showAll()
时,您可能希望按类型对消息进行分组,这可以这样做
{!! \Krucas\Notification\Facades\Notification::group('info', 'success', 'error', 'warning')->showAll() !!}
这将在组中分组所有您的消息并输出它,您也可以只使用一个、两个或三个组。
动态操作组输出
\Krucas\Notification\Facades\Notification::addToGrouping('success')->removeFromGrouping('error');
在默认容器中以类型显示通知,您可以传递自定义格式
{!! \Krucas\Notification\Facades\Notification::showError() !!} {!! \Krucas\Notification\Facades\Notification::showInfo() !!} {!! \Krucas\Notification\Facades\Notification::showWarning() !!} {!! \Krucas\Notification\Facades\Notification::showSuccess(':message') !!}
以自定义格式在特定容器中显示通知。
{!! \Krucas\Notification\Facades\Notification::container('myContainer')->showInfo(':message') !!}
或者,您也可以使用 blade 扩展
@notification() // will render default container @notification('custom') // will render 'custom' container
消息定位
您可以将消息添加到特定位置。
// This will add message at 5th position \Krucas\Notification\Facades\Notification::info(Notification::message('info')->position(5)); \Krucas\Notification\Facades\Notification::info(Notification::message('info2')->position(1);
清除消息
您可以清除所有消息或按类型清除。
\Krucas\Notification\Facades\Notification::clearError(); \Krucas\Notification\Facades\Notification::clearWarning(); \Krucas\Notification\Facades\Notification::clearSuccess(); \Krucas\Notification\Facades\Notification::clearInfo(); \Krucas\Notification\Facades\Notification::clearAll();
添加消息并在视图中立即显示
想在视图中添加消息并显示?非常简单
{!! \Krucas\Notification\Facades\Notification::container('myInstant') ->infoInstant('Instant message added in a view and displayed!') !!}
您还可以添加多个消息
{!! \Krucas\Notification\Facades\Notification::container('myInstant') ->infoInstant('Instant message added in a view and displayed!') ->errorInstant('Error...') !!}