goomcoom/laravel-messages

有时你可能希望返回包含额外信息的响应,以告知用户发生了什么。本软件包提供了一种流畅的接口,可以在代码库的任何位置添加消息,并将其优雅地添加到JSON响应中。

1.0.0 2020-06-17 14:48 UTC

This package is auto-updated.

Last update: 2024-09-18 00:53:20 UTC


README

有时你可能希望返回包含额外信息的响应,以告知用户发生了什么。本软件包提供了一种流畅的接口,可以在代码库的任何位置添加消息,并将其优雅地添加到JSON响应中。

安装

使用composer安装此软件包。

$ composer require goomcoom/laravel-messages

服务提供者和外观会自动注册,但您也可以手动将它们添加到应用配置中。

// config/app.php

[
    'providers' => [
        // ...
        GoomCoom\Messages\MessagesServiceProvider::class,
    ],
    
    'aliases' => [
        // ...
        'Messages' => GoomCoom\Messages\Facades\Messages::class,
    ],
];

配置

要发布配置文件,您可以使用以下命令

$ php artisan vendor:publish --tag=goomcoom-laravel-messages

配置文件包含可用于接收消息的袋子,这些袋子可以完全自定义。

// config/goomcoom-laravel-messages.php

return [
    /**
     * –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
     * Bags
     * –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
     * These are the bags that messages can be added to.
     */

    'bags' => [
        'error',
        'info',
        'success',
        'warning',
    ],
];

将消息添加到响应中

如果您希望消息自动添加到响应中,可以使用AddMessagesToResponse中间件。中间件会检查是否有任何消息,并将它们添加到响应的元内容中。有关使用中间件的详细信息,请参阅官方文档

// The messages are added to the response's meta object

{
    data: {
        ...
    },
    meta: {
        ...
        messages: {
            error: [
                'Resource 532 was not updated',
            ],
            info: [
                'We did something you might not have expected'
            ]
        }
    }
}

中间件还会检查响应是否有消息属性,并将消息追加到meta.messages.error数组中。

// The response message is appended to the error messages array

{
    message: 'Somethig went wrong.',
    meta: {
        messages: {
            error: [
                'Resource 532 was not updated',
                'Something went wrong.'
            ],
        }
    }
}

添加消息

第一个参数是要添加消息的消息袋子。我们使用splat运算符收集消息,因此您可以一次添加多个以逗号分隔的消息。

    Messages::add('error', 'Cannot do that!', 'Something went wrong.');
    Messages::add('info', 'Something else happened.');

    /*
        {
            ...
            meta: {
                messages: {
                    error: [
                        'Cannot do that!',
                        'Something went wrong.'
                    ],
                    info: [
                        'Something else happened.'
                    ]
                }
            }
        }
    */

值得注意的是,消息不会在类别内重复。

获取袋子

该软件包使用laravel的MessageBag类来分类消息。您可以使用getBag方法检索特定袋子。

// Returns Illuminate/Support/MessageBag with "warning" messages
Messages::getBag('warning');

获取所有消息

您还可以使用getAll方法检索所有消息,并将它们作为一个关联数组返回,消息根据其类别分组。

Messages::getAll();

/*
    [
        'error' => [ ... ],
        'info' => [ ... ],
        'success' => [ ... ],
        'warning' => [ ... ],
    ]
*/

检查是否添加了任何消息

您可以使用hasAny方法检查是否添加了任何消息。

// Returns boolean
Messages::hasAny();

删除消息

您可以使用remove方法从特定袋子中删除消息。

// Removes all messages from the success bag.
Messages::remove('success', '*');

// Removes the "To be removed" & "Also to be removed" messages from the error bag'
Messages::remove('error', 'To be removed', 'Also to be removed');

重置所有消息袋子

您可以通过调用reset方法重置所有消息袋子。

// Removes all messages
Messages::reset();