ayd/api-response-hyperf

An API response package for hyperf applications at AYD Company.

Maintainers

Package info

github.com/AYDcompany/api-response-hyperf

pkg:composer/ayd/api-response-hyperf

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.0.0 2026-06-18 12:12 UTC

This package is not auto-updated.

Last update: 2026-06-18 21:53:23 UTC


README

Hyperf integration for the ayd/api-response-base package. Provides a service class and trait for building consistent JSON API responses with PSR-7 response objects.

Installation

composer require ayd/api-response-hyperf

The ConfigProvider is auto-discovered by Hyperf and registers ApiResponse as a dependency.

Usage

1. Dependency Injection

Inject ApiResponse into your controller or service. Call setResponse() with the Hyperf response object before use:

use Hyperf\HttpServer\Contract\ResponseInterface;
use Ayd\ApiResponseHyperf\ApiResponse;

class UserController
{
    public function __construct(
        private ApiResponse $response,
        private ResponseInterface $hyperfResponse,
    ) {
        $this->response->setResponse($this->hyperfResponse);
    }

    public function index()
    {
        return $this->response->success($users);
    }

    public function store()
    {
        // validation...

        return $this->response->created($user, 'User created');
    }

    public function show($id)
    {
        $user = User::find($id);

        if (!$user) {
            return $this->response->notFound('User not found');
        }

        return $this->response->success($user);
    }
}

2. Trait (for Controllers)

Use ApiResponseTrait to call response methods directly. The trait expects the controller to have a $this->response property (the Hyperf ResponseInterface, which is auto-injected in Hyperf controllers):

use Ayd\ApiResponseHyperf\ApiResponseTrait;

class UserController
{
    use ApiResponseTrait;

    public function index()
    {
        return $this->success(User::all());
    }

    public function store()
    {
        // validation...

        return $this->created($user, 'User created');
    }

    public function destroy($id)
    {
        User::findOrFail($id)->delete();

        return $this->noContent();
    }
}

Error Handling

Unlike the Laravel package, all methods return Psr\Http\Message\ResponseInterface — error methods do not throw exceptions. Always return the result:

public function show($id)
{
    $user = User::find($id);

    if (!$user) {
        return $this->notFound('User not found');
    }

    return $this->success($user);
}

Available error methods: fail(), error(), badRequest(), unauthorized(), forbidden(), notFound(), unprocessable().

Abilities Resolver

Bind your resolver in a ConfigProvider or dependency configuration:

use Ayd\ApiResponseBase\Contracts\AbilitiesResolver;

return [
    'dependencies' => [
        AbilitiesResolver::class => MyAbilitiesResolver::class,
    ],
];

Hyperf's DI container will auto-inject it into ApiResponse's constructor.

API Reference

Success Methods

Method HTTP Status Return Type
success($data, $message, $meta) 200 ResponseInterface
created($data, $message, $meta) 201 ResponseInterface
updated($data, $message, $meta) 200 ResponseInterface
accepted($data, $message, $meta) 202 ResponseInterface
noContent() 204 ResponseInterface

Error Methods

Method HTTP Status Return Type
fail($code, $message, $data, $meta) configurable ResponseInterface
error($code, $message, $data, $meta) configurable ResponseInterface
badRequest($message, $data) 400 ResponseInterface
unauthorized($message) 401 ResponseInterface
forbidden($message) 403 ResponseInterface
notFound($message) 404 ResponseInterface
unprocessable($message, $data) 422 ResponseInterface

Requirements

  • PHP ^8.1
  • ayd/api-response-base ^1.0
  • hyperf/http-server ^3.0
  • psr/http-message ^1.0 | ^2.0

License

MIT