PHPackages                             pmarien/rest-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. pmarien/rest-api-response

AbandonedArchivedLibrary[API Development](/categories/api)

pmarien/rest-api-response
=========================

This library provides a extension for the symfony/http-foundation component to build standardized json responses.

2.0.0(10y ago)0142MITPHPPHP &gt;=5.5

Since Oct 30Pushed 10y ago1 watchersCompare

[ Source](https://github.com/pmarien/rest-api-response)[ Packagist](https://packagist.org/packages/pmarien/rest-api-response)[ RSS](/packages/pmarien-rest-api-response/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (3)Versions (6)Used By (0)

Rest API Response Library
=========================

[](#rest-api-response-library)

[![Build Status](https://camo.githubusercontent.com/221b877c90a7c7d3dc75c7e8f3b6f60d2aff8d28171ba3cc0336c086cff69292/68747470733a2f2f7472617669732d63692e6f72672f706d617269656e2f726573742d6170692d726573706f6e73652e737667)](https://travis-ci.org/pmarien/rest-api-response)[![Coverage Status](https://camo.githubusercontent.com/bd1a8fe81dcfa736668a8f596db5ed188575c709470b7fa640576963346cb48b/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f706d617269656e2f726573742d6170692d726573706f6e73652f62616467652e7376673f6272616e63683d646576656c6f7026736572766963653d676974687562)](https://coveralls.io/github/pmarien/rest-api-response?branch=develop)[![SensioLabsInsight](https://camo.githubusercontent.com/70bfc4ef0455110dfba22b1b5c5a23b5ed753aa7326a4786711c3df6e997c0ea/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f33353832303964662d323739322d343235622d616536322d6561636336373432646233632f6d696e692e706e67)](https://insight.sensiolabs.com/projects/358209df-2792-425b-ae62-eacc6742db3c)

This Library provides a Response abstraction for RESTful JSON-API's on top of the Symfony HTTP-Foundation-Component.

The Library defines custom Exceptions and Exception-Interfaces for Error Handling.

There is a Response-Class for handling of successfull responses and an Error-Response-Class for Exception-Responses.

Install with Composer
---------------------

[](#install-with-composer)

```
composer require pmarien/rest-api-response

```

### Version

[](#version)

If you use the symfony/http-foundation in any version lower than 3.0 please use version 1.\* of these library.

If you use the symfony/http-foundation in any version higher than 3.0 please use version 2.\* of these library.

Usage
-----

[](#usage)

### JSON Output

[](#json-output)

**Status**:

*indicates, weather the request was successful or not*

**Count**:

*Number of results which are delivered within the results array*

**Results**:

*Array of one or more data objects (your data)*

#### Success

[](#success)

```
{
    "status": "success",
    "count": 1,
    "results": [
        {
            "custom": "data"
        }
    ]
}

```

#### Error

[](#error)

```
{
    "status": "error",
    "count": 1,
    "results": [
        {
            "code": 0,
            "message": "Error Message",
            "previous": null,
            "data": [
                "optional custom debug data, can be an object or an array"
            ]
        }
    ]
}

```

### Get a Response Object

[](#get-a-response-object)

`$response = new ApiResponse(['message'=>'Hello World']);`

`$errorResponse = new ApiResponse(new \Exception('Request failed!'));`

### Object Handling

[](#object-handling)

By default, protected and private properties of objects will be ignored be json\_encode().

To build a response from custom objects, this library provides an Interface (JsonEncodableInterface). Objects which implement these Interface can be handled on custom way.

#### Example

[](#example)

**your Class**

```
class CustomObject implements JsonEncodableInterface {

    /**
     * @var string
     */
    protected $publicForResponse = 'hello';

    /**
     * @var string
     */
    protected $notPublic = 'world';

    /**
     * Return an array of properties which can be encoded as json
     * @return array
     */
    public function encode(){
        return [
            'public' => $this->publicForResponse
        ];
    }
}

```

**your Controller Action**

```
public function testAction(){
    return new ApiResponse(new CustomObject());
}

```

**Json-Response**

```
{
    "status": "success",
    "count": 1,
    "results": [
        {
            "public": "hello"
        }
    ]
}

```

### Exceptions

[](#exceptions)

There are four special Exceptions and Interfaces defined in this Library:

**ExceptionInterface**:

*Provides a Method called "getMetaData" which should return an array with custom debug data for the error response*

**UncriticalExceptionInterface**:

*If an Exception implements these Interface, the error response will return the error object and status but with a HTTP-Status-Code 200 (Ok)*

**CriticalExceptionInterface**:

*With these Interface you are able to define a custom HTTP-Status-Code instead of the default 500 (Internal Server Error) Status*

**ExceptionListInterface**:

*With these Interface you are able to define more than one error result object for one response*

**Please note that your custom Exception must extend the basic php exception, even if you implement one ore more of the interfaces.**

There are also predefined exceptions ready to use:

**UncriticalException**:

*Implements the ExceptionInterface and the UncriticalExceptionInterface*

**CriticalException**:

*Implements the ExceptionInterface and the CriticalExceptionInterface*

**UncriticalExceptionExceptionList**:

*Implements the ExceptionInterface, the UncriticalExceptionInterface and the ExceptionListInterface*

**CriticalExceptionExceptionList**:

*Implements the ExceptionInterface, the CriticalExceptionInterface and the ExceptionListInterface*

Licence
-------

[](#licence)

This library is under MIT Licence.

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~21 days

Total

4

Last Release

3835d ago

Major Versions

1.0.2 → 2.0.02016-01-02

PHP version history (2 changes)1.0.0PHP &gt;=5.4

2.0.0PHP &gt;=5.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/21ec4e897f4ee0a0d0bcaf844242946502ff852e9bee8fb2e1ef9a2fc2fb5f16?d=identicon)[pmarien](/maintainers/pmarien)

---

Top Contributors

[![pmarien](https://avatars.githubusercontent.com/u/8394874?v=4)](https://github.com/pmarien "pmarien (6 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/pmarien-rest-api-response/health.svg)

```
[![Health](https://phpackages.com/badges/pmarien-rest-api-response/health.svg)](https://phpackages.com/packages/pmarien-rest-api-response)
```

###  Alternatives

[symfony/http-kernel

Provides a structured process for converting a Request into a Response

8.1k869.4M8.8k](/packages/symfony-http-kernel)[statamic/cms

The Statamic CMS Core Package

4.8k3.6M985](/packages/statamic-cms)[sylius/sylius

E-Commerce platform for PHP, based on Symfony framework.

8.5k5.9M736](/packages/sylius-sylius)[easycorp/easyadmin-bundle

Admin generator for Symfony applications

4.3k17.9M388](/packages/easycorp-easyadmin-bundle)[symfony/security-bundle

Provides a tight integration of the Security component into the Symfony full-stack framework

2.5k185.6M2.4k](/packages/symfony-security-bundle)[api-platform/http-cache

API Platform HttpCache component

274.6M19](/packages/api-platform-http-cache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
