laratoolbox/responder

Easily respond to api requests for laravel.

v0.1.0 2021-01-26 12:46 UTC

This package is auto-updated.

Last update: 2024-11-26 21:31:38 UTC


README

Easily respond to api requests.

Requirement

Laravel >= 5.5

Installation

You can install the package via composer:

$ composer require laratoolbox/responder

Usage

After installation, you can use helper responder function.

return responder()
        ->addHeader('X-Secret1', 'secret1')
        ->addHeader('X-Secret2', 'secret2')
        ->addHeader('X-Secret3', 'secret3')
        ->setData(\App\Models\User::select('id', 'name')->find(1))
        ->addExtraData('custom-key', 'custom-value')
        ->send();

See response below:

HTTP/1.1 200 OK
Content-Type: application/json
X-Secret1: secret1
X-Secret2: secret2
X-Secret3: secret3

{
    "code": 0,
    "message": null,
    "data": {
        "id": 1,
        "name": "Semih ERDOGAN"
    },
    "custom-key": "custom-value"
}

Also, you have ResponderException that you can throw it any time required.

throw new \LaraToolbox\Responder\Exceptions\ResponderException(
    \LaraToolbox\Responder\ResponseCodes::ERROR,
    $customData = [1,2,3]
);

See response below:

HTTP/1.1 200 OK
Content-Type: application/json

{
    "code": 120,
    "message": "An error occurred",
    "data": [
        1,
        2,
        3
    ]
}

All methods that responder has.

send

  • Parameter null|mixed $data (if set calls setData method behind the scenes)

setHttpStatusCode

  • Parameter int $statusCode

setResponseMeta

  • Parameter array $responseMeta

setResponseCode

  • Parameter int $responseCode

setResponseMessage

  • Parameter string $responseMessage

setData

  • Parameter mixed $data

addExtraData

  • Parameter string $key
  • Parameter mixed $value

addHeader

  • Parameter string $key
  • Parameter string|integer|numeric $value

Testing

// TODO:

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email hasansemiherdogan@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.