承接 goomcoom/laravel-messages 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

goomcoom/laravel-messages

Composer 安装命令:

composer require goomcoom/laravel-messages

包简介

Sometimes you may wish to return responses with some extra messages informing the user of what happened. This package provides a fluent interface to add messages from anywhere within in your code base and gracefully add them to your JSON responses.

README 文档

README

Sometimes you may wish to return responses with some extra messages informing the user of what happened. This package provides a fluent interface to add messages from anywhere within in your code base and gracefully add them to your JSON responses.

Installation

Install the package using composer.

$ composer require goomcoom/laravel-messages

The service provider and facade are registered automatically, but you may do so manually by adding them to the app config.

// config/app.php

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

Config

To publish the config file you may use the following command

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

The config file holds the bags that are available for accepting messages which are fully customizable.

// config/goomcoom-laravel-messages.php

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

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

Adding messages to responses

If you would like the messages to the added to the response automatically, you may use the AddMessagesToResponse middleware. The middleware checks if there are any messages and adds them to the response's meta content. For information on using middleware please refer to the official documentation.

// 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'
            ]
        }
    }
}

The middleware also checks if the response has a message property and appends the message to the meta.messages.error array.

// 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.'
            ],
        }
    }
}

Adding messages

The first argument is the message bag that the messages are meant to be added to. We use the splat operator to gather messages, so you may add multiple comma-separated messages at once.

    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.'
                    ]
                }
            }
        }
    */

It's worth noting that messages are not duplicated within a category.

Getting a bag

The package uses laravel's MessageBag class to categorise the messages. You may retrieve a specific bag using the getBag method.

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

Getting all messages

You may also retrieve all the messages as an associative array with the messages grouped by their category by using the getAll method.

Messages::getAll();

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

Checking if any messages have been added

You may check if any messages have been added by using the hasAny method.

// Returns boolean
Messages::hasAny();

Removing messages

You may remove messages from a specific bag by using the remove method.

// 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');

Resetting all message bags

You may reset all message bags by calling the reset method.

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

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固