gaaarfild/laravel-notifications

此包已被弃用且不再维护。作者建议使用 garf/laravel-notifications 包。

便捷的闪存通知

2.1.1 2017-01-26 08:49 UTC

This package is auto-updated.

Last update: 2022-02-01 12:49:43 UTC


README

Laravel Version Packagist Licence Build Status

Laravel Notifications

俄语文档 / Русская документация

Laravel 5 的通知系统。

通常在保存用户数据后,您需要重定向到适当的页面,并通知用户操作成功或出现错误。

现在您可以通过更便捷和简单的方式完成这些操作。

安装

要安装,请在控制台执行以下命令

$ composer require "garf/laravel-notifications:2.*"

完成后,将以下内容添加到您的 config/app.php 文件中的 providers 部分

'providers' => [
    // ...
    Garf\LaravelNotifications\LaravelNotificationsServiceProvider::class,
]

如果您想使用 Notifications 门面,请将以下内容添加到同一文件的 aliases 部分

'aliases' => [
    // ...
  'Notifications' => Garf\LaravelNotifications\NotificationsFacade::class,
]

要更改模板,请在控制台执行以下命令

php artisan vendor:publish --provider="Garf\LaravelNotifications\LaravelNotificationsServiceProvider" --tag="config"

现在您可以在 config/laravel-notifications.php 中设置任何用于通知渲染的视图文件。

可选地,您可以在控制台执行以下命令以编辑默认模板,而不是使用自己的模板

php artisan vendor:publish --provider="Garf\LaravelNotifications\LaravelNotificationsServiceProvider" --tag="views"

注意:如果您发布视图并编辑它,不要更改配置文件中视图的名称。

用法

为下一个请求保存消息

Notifications::add('Your message text', 'type', 'group');

$type 参数特别用于 Twitter Bootstrap 渲染。它显示带有相应类的警报。

例如,如果您将 type 设置为 danger,则在 toBootstrap() 格式方法中会生成类为 'alert alert-danger' 的警报。

$group 参数将消息分组。在下一个请求中,您可以按组检索它们。

还可以使用更便捷的别名

    Notifications::info('Your message text', 'group');
    Notifications::success('Your message text', 'group');
    Notifications::warning('Your message text', 'group');
    Notifications::danger('Your message text', 'group');
    Notifications::error('Your message text', 'group');

检索消息

所有消息

Notifications::all()->get();

按组消息

Notifications::byGroup('my-group')->get();

按类型消息

Notifications::byType('warning')->get();

格式化消息

您还可以格式化消息

JSON

您可以以 JSON 格式检索和格式化所有消息

Notifications::all()->toJson();

或根据组或类型进行筛选

Notifications::byType('success')->toJson();

Notifications::byGroup('login')->toJson();

使用 blade 视图文件渲染

您还可以使用自定义视图文件渲染通知

{{ Notifications::all() }}

您还可以根据组或类型进行筛选

{{ Notifications::byType('info') }}

{{ Notifications::byGroup('registration') }}

默认方法使用 Twitter Bootstrap 警报格式。

Twitter Bootstrap 可选。

其他

消息计数

Notifications::all()->count();

检查消息是否存在

Notifications::all()->has();

获取第一条消息

Notifications::all()->first();

表单请求的使用

如果您想在Laravel表单请求中显示错误,您必须在您的表单请求类中重写formatErrors()方法。

    public function formatErrors(Validator $validator){
        foreach ($validator->errors()->all() as $error) {
            Notifications::add($error, 'danger');
        }

        return $validator->errors()->getMessages();
    }

不要忘记在文件头部导入Validator

use Illuminate\Contracts\Validation\Validator;

贡献

贡献非常受欢迎。

将您的拉取请求发送到master分支。

许可协议

MIT许可协议(MIT)。有关更多信息,请参阅许可文件