PHPackages                             dragon-code/api-response - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. dragon-code/api-response

AbandonedArchivedLibrary[API Development](/categories/api)

dragon-code/api-response
========================

Package for standardizing the responses from the API of your Symfony based applications.

v10.0.0(4y ago)371.7k[2 PRs](https://github.com/TheDragonCode/api-response/pulls)MITPHPPHP ^8.0

Since Feb 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/TheDragonCode/api-response)[ Packagist](https://packagist.org/packages/dragon-code/api-response)[ Fund](https://paypal.me/helldar)[ Fund](https://yoomoney.ru/to/410012608840929)[ RSS](/packages/dragon-code-api-response/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (5)Versions (63)Used By (0)

API Response
============

[](#api-response)

[![API Response](https://camo.githubusercontent.com/30bf65ed5e9de64045620cd6ce1691da4754050a8b9f7ed984ba755b82501d29/68747470733a2f2f707265766965772e647261676f6e2d636f64652e70726f2f546865447261676f6e436f64652f6170692d726573706f6e73652e7376673f6272616e643d706870)](https://camo.githubusercontent.com/30bf65ed5e9de64045620cd6ce1691da4754050a8b9f7ed984ba755b82501d29/68747470733a2f2f707265766965772e647261676f6e2d636f64652e70726f2f546865447261676f6e436f64652f6170692d726573706f6e73652e7376673f6272616e643d706870)

[![Stable Version](https://camo.githubusercontent.com/3f41192a4de979ce1f3b305405ad9341c57903e1755161ed9c42cd5ead0e4745/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f546865447261676f6e436f64652f6170692d726573706f6e73653f6c6162656c3d737461626c65267374796c653d666c61742d737175617265)](https://packagist.org/packages/dragon-code/api-response)[![Unstable Version](https://camo.githubusercontent.com/85afbac0745eb275d863cee4b1701a70c0e30315d27990b00f1abd553b8761e8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f756e737461626c652d6465762d2d6d61696e2d6f72616e67653f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dragon-code/api-response)[![Total Downloads](https://camo.githubusercontent.com/52fd5081bb6fea1bffc768765374b478360a5d1dd614fef20c19adf9e483f939/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f647261676f6e2d636f64652f6170692d726573706f6e73652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dragon-code/api-response)[![License](https://camo.githubusercontent.com/d89408fbd6e7068fc2fd56e865bd10dd34f7abdb0345aed808825818ad6fcbbc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f647261676f6e2d636f64652f6170692d726573706f6e73652e7376673f7374796c653d666c61742d737175617265)](LICENSE)

> Package for standardizing the responses from the API of your **Symfony based** applications.

Getting Started
---------------

[](#getting-started)

### Upgrade guides

[](#upgrade-guides)

- [To 10.x From 9.x](.upgrading/9.x_10.x.md)
- [To 9.x From 8.x and from the `andrey-helldar/api-response` package](.upgrading/8.x_9.x.md)
- [To 8.x From 7.x](.upgrading/7.x_8.x.md)
- [To 7.x From 6.x](.upgrading/6.x_7.x.md)
- [To 6.x From 5.x](.upgrading/5.x_6.x.md)

[\[ to top \]](#api-response)

### Installation

[](#installation)

To get the latest version of `API Response`, simply require the project using [Composer](https://getcomposer.org/):

```
$ composer require dragon-code/api-response
```

This command will automatically install the latest version of the package for your environment.

Instead, you may of course manually update your `require` block and run `composer update` if you so choose:

```
{
    "require": {
        "dragon-code/api-response": "^9.1"
    }
}
```

Alright! Use `api_response()` helper.

### Compatibility table

[](#compatibility-table)

Package versionPHP min versionSymfony versionSupportLinks^9.07.2.5^4.0, ^5.0, ^6.0[![Supported](https://camo.githubusercontent.com/ad8d24cfab47bf6c16d0ac063c4cb694237ea9d37bf2c3884bacc7e64651724b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f737570706f727465642d677265656e3f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/ad8d24cfab47bf6c16d0ac063c4cb694237ea9d37bf2c3884bacc7e64651724b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f737570706f727465642d677265656e3f7374796c653d666c61742d737175617265)[Upgrade guide](.upgrading/8.x_9.x.md)^8.07.2.5^4.0, ^5.0[![Not Supported](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)[Upgrade guide](.upgrading/7.x_8.x.md)^7.07.2.5^4.0, ^5.0[![Not Supported](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)[Upgrade guide](.upgrading/6.x_7.x.md)^6.07.3^4.0, ^5.0[![Not Supported](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)[Upgrade guide](.upgrading/5.x_6.x.md)^5.07.1.3^4.0, ^5.0[![Not Supported](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)---^4.4.15.6.9^3.0, ^4.0, ^5.0[![Not Supported](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)---^4.05.6.9^3.0, ^4.0[![Not Supported](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)](https://camo.githubusercontent.com/a880e87921f3951c5d38170a7c772f620e451cc21444f83382a7799831e01d81/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6e6f742d2d737570706f727465642d6c69676874677265793f7374796c653d666c61742d737175617265)---[\[ to top \]](#api-response)

Using
-----

[](#using)

### Use with `data` key

[](#use-with-data-key)

#### as NULL with code:

[](#as-null-with-code)

```
// php 7.4 and below
return api_response(null, 304);

// php 8.0
return api_response( status_code: 304 );
```

return with code 304:

```
{
    "data": null
}
```

[\[ to top \]](#api-response)

#### as integer with default code:

[](#as-integer-with-default-code)

```
return api_response(304);
```

return with code 200:

```
{
    "data": 304
}
```

[\[ to top \]](#api-response)

#### as string with default code:

[](#as-string-with-default-code)

```
return api_response('qwerty');
```

return with code 200:

```
{
    "data": "qwerty"
}
```

[\[ to top \]](#api-response)

#### as string with code:

[](#as-string-with-code)

```
return api_response('qwerty', 400);
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": "qwerty"
    }
}
```

[\[ to top \]](#api-response)

#### as integer with code:

[](#as-integer-with-code)

```
return api_response(304, 400);
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": 304
    }
}
```

[\[ to top \]](#api-response)

#### as array:

[](#as-array)

```
$data = [
    [
        'title' => 'Title #1',
        'description' => 'Description #1',
    ],
    [
        'title' => 'Title #2',
        'description' => 'Description #2',
    ],
];
```

[\[ to top \]](#api-response)

#### as error

[](#as-error)

```
return api_response($data, 400);
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": [
            {
                "title": "Title #1",
                "description": "Description #1"
            },
            {
                "title": "Title #2",
                "description": "Description #2"
            }
        ]
    }
}
```

[\[ to top \]](#api-response)

#### as success

[](#as-success)

```
return api_response($data);
```

return with code 200:

```
{
    "data": [
        {
            "title": "Title #1",
            "description": "Description #1"
        },
        {
            "title": "Title #2",
            "description": "Description #2"
        }
    ]
}
```

If the first parameter is a number, then the decryption of the error by code will be return. In other cases, the value of the passed variable will be return.

[\[ to top \]](#api-response)

#### with additional content

[](#with-additional-content)

```
// php 7.4 and below
return api_response('title', 200, ['foo' => 'bar']);
// or
return api_response('title', null, ['foo' => 'bar']);

// php 8.0
return api_response('title', with: ['foo' => 'bar']);
```

return with code 200:

```
{
    "data": "title",
    "foo": "bar"
}
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": "ok"
    },
    "foo": "bar"
}
```

```
return api_response(['data' => 'foo', 'bar' => 'baz']);
```

return with code 200:

```
{
    "data": "foo",
    "bar": "baz"
}
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": "foo"
    },
    "bar": "baz"
}
```

[\[ to top \]](#api-response)

### Use without `data` key

[](#use-without-data-key)

Since the goal of the package is to unify all the answers, we moved the variable definitions into a static function. So, for example, to enable or disable wrapping content in the `data` key, you need to call the `wrapped` or `withoutWrap` method:

```
use DragonCode\ApiResponse\Services\Response;

Response::withoutWrap();
```

#### as NULL with code and without `data` key:

[](#as-null-with-code-and-without-data-key)

```
// php 7.4 and below
return api_response(null, 304);

// php 8.0
return api_response( status_code: 304 );
```

return with code 304:

```
[]
```

[\[ to top \]](#api-response)

#### as integer with default code and without `data` key:

[](#as-integer-with-default-code-and-without-data-key)

```
return api_response(304, 200);
```

return with code 200:

```
304
```

[\[ to top \]](#api-response)

#### as string with default code and without `data` key:

[](#as-string-with-default-code-and-without-data-key)

```
return api_response('qwerty', 200);
```

return with code 200:

```
"qwerty"
```

[\[ to top \]](#api-response)

#### as string with code and without `data` key:

[](#as-string-with-code-and-without-data-key)

```
return api_response('qwerty', 400);
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": "qwerty"
    }
}
```

[\[ to top \]](#api-response)

#### as integer with code and without `data` key:

[](#as-integer-with-code-and-without-data-key)

```
return api_response(304, 400);
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": 304
    }
}
```

[\[ to top \]](#api-response)

#### as array and without `data` key:

[](#as-array-and-without-data-key)

```
$data = [
    [
        'title' => 'Title #1',
        'description' => 'Description #1',
    ],
    [
        'title' => 'Title #2',
        'description' => 'Description #2',
    ],
];
```

#### as error and without `data` key

[](#as-error-and-without-data-key)

```
return api_response($data, 400);
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": [
            {
                "title": "Title #1",
                "description": "Description #1"
            },
            {
                "title": "Title #2",
                "description": "Description #2"
            }
        ]
    }
}
```

[\[ to top \]](#api-response)

#### as success and without `data` key

[](#as-success-and-without-data-key)

```
return api_response($data, 200);
// or
return api_response($data);
```

return with code 200:

```
[
    {
        "title": "Title #1",
        "description": "Description #1"
    },
    {
        "title": "Title #2",
        "description": "Description #2"
    }
]
```

If the first parameter is a number, then the decryption of the error by code will be return. In other cases, the value of the passed variable will be return.

[\[ to top \]](#api-response)

#### with additional content and without `data` key:

[](#with-additional-content-and-without-data-key)

```
// php 7.4 and below
return api_response('title', 200, ['foo' => 'bar']);

// php 8.0
return api_response('title', with: ['foo' => 'bar']);
```

return with code 200:

```
{
    "data": "title",
    "foo": "bar"
}
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": "ok"
    },
    "foo": "bar"
}
```

```
return api_response(['data' => 'foo', 'bar' => 'baz']);
```

return with code 200:

```
{
    "data": "foo",
    "bar": "baz"
}
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": "foo"
    },
    "bar": "baz"
}
```

[\[ to top \]](#api-response)

### No extra data

[](#no-extra-data)

In some cases, when returning answers, you must also give additional data. Such as stack trace, for example.

To prevent this data from getting in response to production, you can globally set a label to show or hide this data:

```
use DragonCode\ApiResponse\Services\Response;

env('APP_DEBUG')
    ? Response::allowWith()
    : Response::withoutWith();
```

Now all responses will not contain the additional data being passed.

For example:

```
// php 7.4 and below
return api_response('title', 200, ['foo' => 'bar']);
// or
return api_response('title', null, ['foo' => 'bar']);

// php 8.0
return api_response('title', with: ['foo' => 'bar']);
```

return with code 200:

```
{
    "data": "title"
}
```

return with code 400:

```
{
    "error": {
        "type": "Exception",
        "data": "ok"
    }
}
```

#### Server Errors

[](#server-errors)

> Note: The `$with` parameter is also responsible for displaying server-side error messages.
>
> In this case, Http errors will be displayed without masking.

For example:

```
use DragonCode\ApiResponse\Services\Response;

Response::allowWith();

$e = new Exception('Foo', 0);

return api_response($e);
```

return with code 500:

```
{
    "error": {
        "type": "Exception",
        "data": "Foo"
    }
}
```

and

```
use DragonCode\ApiResponse\Services\Response;

Response::withoutWith();

$e = new Exception('Foo', 0);

return api_response($e);
```

return with code 500:

```
{
    "error": {
        "type": "Exception",
        "data": "Whoops! Something went wrong."
    }
}
```

return with if code &gt;=400 and &lt; 500:

```
{
    "error": {
        "type": "Exception",
        "data": "Foo"
    }
}
```

[\[ to top \]](#api-response)

### Returning exception instances

[](#returning-exception-instances)

```
class FooException extends \Exception
{
    public function __construct()
    {
        parent::__construct('Foo', 405);
    }
}

class BarException extends \Exception
{
    public function __construct()
    {
        parent::__construct('Bar');
    }
}

$foo = new FooException();
$bar = new BarException();
```

```
return api_response($foo);
```

return with code 405:

```
{
    "error": {
        "type": "FooException",
        "data": "Foo"
    }
}
```

```
return api_response($foo, 408);
```

return with code 408:

```
{
    "error": {
        "type": "FooException",
        "data": "Foo"
    }
}
```

```
return api_response($bar);
```

return with code 400:

```
{
    "error": {
        "type": "BarException",
        "data": "Bar"
    }
}
```

```
return api_response($bar, 408);
```

return with code 408:

```
{
    "error": {
        "type": "BarException",
        "data": "Bar"
    }
}
```

You can also add additional data:

```
return api_response($foo, 405, ['foo' => 'Bar']);
// or
return api_response($foo, 0, ['foo' => 'Bar']);
```

return with code 405:

```
{
    "error": {
        "type": "FooException",
        "data": "Foo"
    },
    "foo": "Bar"
}
```

[\[ to top \]](#api-response)

### Best practice use with the Laravel and Lumen Frameworks

[](#best-practice-use-with-the-laravel-and-lumen-frameworks)

If you use the Laravel or Lumen framework, you can update the `extends` in the `app\Exceptions\Handler.php` file depending on your application version and needs:

Version \\ TypeAPI + WEBOnly API9.x`DragonCode\ApiResponse\Exceptions\Laravel\Nine\Handler as ExceptionHandler``DragonCode\ApiResponse\Exceptions\Laravel\Nine\ApiHandler as ExceptionHandler`8.x`DragonCode\ApiResponse\Exceptions\Laravel\Eight\Handler as ExceptionHandler``DragonCode\ApiResponse\Exceptions\Laravel\Eight\ApiHandler as ExceptionHandler`7.x`DragonCode\ApiResponse\Exceptions\Laravel\Seven\Handler as ExceptionHandler``DragonCode\ApiResponse\Exceptions\Laravel\Seven\ApiHandler as ExceptionHandler`If you did not add anything to this file, then delete everything properties and methods.

For example, as a result, a clean file will look like this:

```
