ilyatos/laravel-api-response

以 JSON 格式提供给我!

dev-master 2021-01-23 18:44 UTC

This package is auto-updated.

Last update: 2024-09-30 00:27:53 UTC


README

欢迎使用 JsonResponse 的简单封装!

要求

  • Laravel 5/6/7

您无需手动注册服务提供者。

使用示例

您需要做的只是将 \Ilyatos\ApiResponse\Contracts\Response 接口注入到控制器/中间件等的构造函数中。
只需看看这个示例,一切就会变得清晰明了。

<?php

declare(strict_types=1);

namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller as BaseController;
use Ilyatos\ApiResponse\Contracts\Response;

class Controller extends BaseController
{
    /**
     * @var Response
     */
    protected $response;

    public function __construct(Response $response)
    {
        $this->response = $response;
    }

    public function example(): JsonResponse
    {
        return $this->response->withMessage('hello!');
    }

    public function anotherExample(): JsonResponse
    {
        return $this->response->withDefaultMessage(\Symfony\Component\HttpFoundation\Response::HTTP_NOT_FOUND);
    }

    public function yetAnotherExample(): JsonResponse
    {
        return $this->response->withData(['status' => 'nice']);
    }
}