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

ActiveLibrary[API Development](/categories/api)

lassehaslev/api-response
========================

Helper class for giving JSON api response

0.1.3(9y ago)01001MITPHPPHP &gt;=5.4.0

Since Jul 10Pushed 9y ago1 watchersCompare

[ Source](https://github.com/LasseHaslev/api-response)[ Packagist](https://packagist.org/packages/lassehaslev/api-response)[ RSS](/packages/lassehaslev-api-response/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (2)Versions (6)Used By (1)

api-response
============

[](#api-response)

> Helper class for giving JSON api response

Motivation
----------

[](#motivation)

Its hard to allways write api response for everyting, and this package will make the job much easier.

This package and its base concept is greatly inspired by [dingo/api](https://github.com/dingo/api). And uses most of the same transformer syntax as [Fractal](http://fractal.thephpleague.com/transformers/).

Install
-------

[](#install)

I use this package mainly in my [Laravel](https://laravel.com/) projects.

Run `composer require lassehaslev/api-response` in your project folder

Usage
-----

[](#usage)

#### Responder

[](#responder)

The Responder is where we call what items we want transformed to JsonResponse. This class need to implement the `ResponseTrait` and will then make the `$this->response` object available. This will make it posible to use `$response->item()` and `$response->collection()`. The first argument for both functions are the data to be transformed, the second argument is the transformer to transform the data. Read below for more information.

```
use LasseHaslev\ApiResponse\Responses\ResponseTrait;
class ResponseCaller
{
    use ResponseTrait;

    public function getItem() {
        return $this->response->item( [ 'name'=>'Test name' ], new Transformer );
    }

    public function getCollection() {
        return $this->response->collection( [
            [ 'name'=>'Test name' ],
            [ 'name'=>'Another test name' ]
        ], new Transformer );
    }

}
```

#### Transformer

[](#transformer)

The transformer is where we format the data to the api. All you have to do is to create a `public function transform` with the model you want to transformed. Then you return an `array` with the data.

You can also have the transformer include more data. This can be useful when model has data. This is done by two different types `default` and `available`, but both types works the same way: Update the array of the type example: `protected $defaultIncludes = ['name']`. Then you need to create a public function thats has name prefix `include`. Example: `public function includeName( $model )`.

###### Default includes

[](#default-includes)

The default include will be included by default.

###### Available includes

[](#available-includes)

The available include will only be included when url parameter `include` are provided. Example `/api/request?include=name`

If you have multiple available includes you can include them by having url request like `/api/request?include=first,second`

```
use LasseHaslev\ApiResponse\Transformers\Transformer as BaseTransformer;
class Transformer extends BaseTransformer
{

    protected $defaultIncludes = [ 'default' ];
    protected $availableIncludes = [ 'available' ];

    /**
     * Transform $model
     *
     * @return Array
     */
    public function transform( $model )
    {
        return [
            'name'=>$model[ 'name' ],
        ];
    }

    /**
     * Return include default
     *
     * @return Array
     */
    public function includeDefault($model)
    {
        return [ 'name'=>'Include default' ];
    }

    /**
     * Return include available
     *
     * @return Array
     */
    public function includeAvailable($model)
    {
        return [ 'name'=>'Include available' ];
    }

}
```

License
-------

[](#license)

MIT, dawg

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 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 ~0 days

Total

4

Last Release

3643d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/39edaff7913420649a4d7e159faecfef8210b583f645ef2f89545f2c72b64a9b?d=identicon)[LasseHaslev](/maintainers/LasseHaslev)

---

Top Contributors

[![LasseHaslev](https://avatars.githubusercontent.com/u/3670799?v=4)](https://github.com/LasseHaslev "LasseHaslev (20 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[symfony/http-kernel

Provides a structured process for converting a Request into a Response

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

The Statamic CMS Core Package

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

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

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

Admin generator for Symfony applications

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

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

2.5k185.6M2.3k](/packages/symfony-security-bundle)[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k51.2M332](/packages/api-platform-core)

PHPackages © 2026

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