edvinaskrucas / notification
Laravel 用于管理闪存/即时通知/消息的包。
Requires
- php: ^5.6|^7.0
- illuminate/session: ^5.4
- illuminate/support: ^5.4
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~5.7
README
该包正在寻找维护者。如果您感兴趣,请与我联系。
Laravel4 / Laravel5 的通知包
Laravel4 的简单通知管理包。
- 通知容器
- 通知集合
- 通知消息
- 通知格式
- 闪存/即时通知
- 方法链
- 消息定位
安装
只需通过 composer.json 在您的 Laravel 安装中添加新的包。
"edvinaskrucas/notification": "5.*"
然后运行 composer update
版本矩阵
注册以使用 Laravel
将以下行添加到 app/config/app.php
ServiceProvider 数组
\Krucas\Notification\NotificationServiceProvider::class,
Kernel 中间件数组(必须在 '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...') !!}