承接 sinema/json-api-error-laravel 相关项目开发

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

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

sinema/json-api-error-laravel

最新稳定版本:0.5

Composer 安装命令:

composer require sinema/json-api-error-laravel

包简介

Format generic JSON API errors for Laravel

README 文档

README

Spec

https://jsonapi.org/examples/#error-objects-basics

Installation

composer require sinema/json-api-error-laravel

Usage

In your Laravel controller:

<?php

namespace App\Http\Controllers;

use Sinemah\JsonApi\Error\Error;
use Sinemah\JsonApi\Error\Responses\Laravel;
use Illuminate\Http\JsonResponse;

class AnyController extends Controller
{
    public function show(): JsonResponse
    {
        return Laravel::get()->json(
            Error::fromArray(
                [
                    'status' => 404,
                    'source' => null,
                    'title' => 'Item not found',
                    'detail' => sprintf('Item %s not found', request('item_uuid')),
                ]
            ),
            404
        );
    }
}

Response

{
    "errors": [
        {
            "status": 404,
            "title": "Item not found",
            "detail": "Item bd11f048-8663-4d95-8c7a-02a5579b0682 not found in customer data"
        }
    ]
}

Build an error stack.

<?php

namespace App\Http\Controllers;

use Sinemah\JsonApi\Error\Error;
use Sinemah\JsonApi\Error\Responses\Laravel;
use Illuminate\Http\JsonResponse;

class AnyController extends Controller
{
    public function show(): JsonResponse
    {
        return Laravel::get()
            ->add(Error::fromArray(['status' => 500, 'code' => 'first_error']))
            ->add(Error::fromArray(['status' => 500, 'code' => 'second_error']))
            ->add(Error::fromArray(['status' => 500, 'code' => 'third_error']))
            ->json();
    }
}

Response

{
    "errors": [
        {
            "status": 500,
            "code": "first_error"
        },
        {
            "status": 500,
            "code": "second_error"
        },
        {
            "status": 500,
            "code": "third_error"
        }
    ]
}

Laravel Response

You do not need to pass a status code via the json method. The status code will be fetched from the first error you pushed in the bag.

->json()

You can also overwrite the status code with the json method.

->json(null, 401)

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-17