PHPackages                             paknahad/jsonapi-bundle - 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. paknahad/jsonapi-bundle

ActiveSymfony-bundle[API Development](/categories/api)

paknahad/jsonapi-bundle
=======================

The jsonapi-bundle is a Symfony bundle. it is the fastest way to generate API based on JsonApi.org using woohoolabs/yin

v5.0.0(4y ago)7155.8k↓16.7%26[18 issues](https://github.com/paknahad/jsonapi-bundle/issues)4MITPHPPHP &gt;=7.1.3

Since Sep 9Pushed 2y ago4 watchersCompare

[ Source](https://github.com/paknahad/jsonapi-bundle)[ Packagist](https://packagist.org/packages/paknahad/jsonapi-bundle)[ RSS](/packages/paknahad-jsonapi-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (20)Versions (33)Used By (4)

[![Latest Stable Version](https://camo.githubusercontent.com/8cf41783278235a028f2f3cdbee5c35ed09ed8b6507bdeaeca186d24eaade884/68747470733a2f2f706f7365722e707567782e6f72672f70616b6e616861642f6a736f6e6170692d62756e646c652f76657273696f6e)](https://packagist.org/packages/paknahad/jsonapi-bundle)[![test workflow](https://github.com/paknahad/jsonapi-bundle/actions/workflows/ci.yaml/badge.svg)](https://github.com/paknahad/jsonapi-bundle/actions/workflows/ci.yaml/badge.svg)[![License: MIT](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](https://choosealicense.com/licenses/mit/)[![Total Downloads](https://camo.githubusercontent.com/fd7681987547001f6af1c2af29c2172c7ae37a62d35bf313caf706d0e8d7469b/68747470733a2f2f706f7365722e707567782e6f72672f70616b6e616861642f6a736f6e6170692d62756e646c652f646f776e6c6f616473)](https://packagist.org/packages/paknahad/jsonapi-bundle)

JsonApiBundle For Symfony
=========================

[](#jsonapibundle-for-symfony)

JsonApiBundle is a [Symfony](https://symfony.com/) bundle. It is the fastest way to generate API based on [JsonApi](http://jsonapi.org/)using [woohoolabs/yin](https://github.com/woohoolabs/yin) Library.

Installing
----------

[](#installing)

1. Install symfony

    ```
    composer create-project symfony/skeleton YOUR_PROJECT

    ```
2. Install the [maker bundle](https://symfony.com/doc/current/bundles/SymfonyMakerBundle/index.html)

    ```
    composer require symfony/maker-bundle phootwork/collection --dev

    ```
3. Install the bundle

    ```
    composer require paknahad/jsonapi-bundle

    ```

    For Symfony 4 use:

    ```
    composer require paknahad/jsonapi-bundle:^3.1

    ```
4. Add below line to `config/bundles.php`

    ```
    Paknahad\JsonApiBundle\JsonApiBundle::class => ['all' => true],

    ```

Usage
-----

[](#usage)

1. Use below command to generate entities one by one:

    ```
    bin/console make:entity

    ```

    for example, Book and Author entity is as follows:

    ```
    use Doctrine\ORM\Mapping as ORM;
    class Book
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;

        /**
         * @ORM\Column(type="string", length=255)
         */
        private $title;

        /**
         * @ORM\Column(type="string", length=20, nullable=true)
         */
        private $isbn;

        /**
         * @ORM\ManyToMany(targetEntity="App\Entity\Author", inversedBy="books")
         */
        private $authors;

        ...
    ```

    ```
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;
    class Author
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         */
        private $id;

        /**
         * @ORM\Column(type="string", length=255)
         * @Assert\NotBlank()
         * @Assert\Length(min=3)
         */
        private $name;

        /**
         * @ORM\ManyToMany(targetEntity="App\Entity\Book", mappedBy="authors")
         */
        private $books;

        ...
    ```
2. Generate CRUD API:

    ```
    bin/console make:api

    ```
3. You can find the generated "collections" for [postman](https://www.getpostman.com/) and [swagger](https://swagger.io/) in the following path and then test the API:

    ```
    collection/postman.json
    collection/swagger.yaml

    ```

Features
--------

[](#features)

### Pagination

[](#pagination)

```
http://example.com/books?page[number]=5&page[size]=30

```

### Sorting

[](#sorting)

- Ascending on name field: `http://example.com/books?sort=name`
- Decending on name field: `http://example.com/books?sort=-name`
- Multiple fields: `http://example.com/books?sort=city,-name`
- Field on a relation: `http://example.com/books?sort=author.name`

### Relationships

[](#relationships)

```
http://example.com/books?include=authors

```

multiple relationships

```
http://example.com/books?include=authors.phones,publishers

```

### Search

[](#search)

As the [JSON API specification](http://jsonapi.org/) does not [specify exactly how filtering should work](http://jsonapi.org/recommendations/#filtering) different methods of filtering can be used. Each method is supplied with a Finder service. Each registered Finder will be able to append conditions to the search query. If you register multiple Finders they are all active at the same time. This enables your API to support multiple filtering methods.

#### Basic Finder.

[](#basic-finder)

A basic Finder is included in this library offering simple filtering capabilities:

This request will return all the books that author's name begin with `hamid`

```
http://example.com/books?filter[authors.name]=hamid%

```

Below line has additional condition: books which have "php" in their title.

```
http://example.com/books?filter[title]=%php%&filter[authors.name]=hamid%

```

#### Setting a default filter on the IndexAction

[](#setting-a-default-filter-on-the-indexaction)

By using `$resourceCollection->getQuery()` you can get access on the query object. use "r" alias for referring to the current entity. in this example the "r" refers to the "ProjectEntity"

```
use Symfony\Component\Routing\Annotation\Route;
class ProjectController extends Controller
{
    /**
     * @Route("/", name="projects_index", methods="GET")
     */
    public function index(ProjectRepository $projectRepository, ResourceCollection $resourceCollection): ResponseInterface
    {
        $resourceCollection->setRepository($projectRepository);

        $resourceCollection->getQuery()->where('r.user_id = :s1')->setParameter(...);
        $resourceCollection->handleIndexRequest();

        return $this->jsonApi()->respond()->ok(
            new ProjectsDocument(new ProjectResourceTransformer()),
            $resourceCollection
        );
    }
```

#### Other Finders

[](#other-finders)

Currently, the following Finders are available via other bundles:

- [mnugter/jsonapi-rql-finder-bundle](https://github.com/mnugter/jsonapi-rql-finder-bundle) - [RQL](https://github.com/persvr/rql) based Finder
- [paknahad-jsonapi-querifier-bundle](https://github.com/paknahad/jsonapi-querifier-bundle) - [Querifier](https://github.com/paknahad/querifier) based Finder

#### Creating a custom Finder

[](#creating-a-custom-finder)

A Finder can be registered via a service tag in the services definition. The tag `paknahad.json_api.finder` must be added to the service for the Finder to be resigered.

Example:

```

```

Each Finder must implement the `Paknahad\JsonApiBundle\Helper\Filter\FinderInterface` interface. Take a look at `\Paknahad\JsonApiBundle\Helper\Filter\Finder` for an implementation example.

If you need more control over the finders, you can instead use `\Paknahad\JsonApiBundle\Helper\Filter\FinderSupportsInterface`interface and implement conditional logic inside `supports()` method:

```
use Paknahad\JsonApiBundle\Helper\Filter\FinderSupportsInterface;
use Paknahad\JsonApiBundle\Helper\FieldManager;
use Symfony\Component\HttpFoundation\Request;

class CustomFinder implements FinderSupportsInterface
{
    public function supports(Request $request, FieldManager $fieldManager): bool
    {
        // based on some request data
        if ($request->query->has('some-flag')) {
            return true;
        }

        // based on document field manager
        if ($fieldManager->getRootEntity() === Author::class) {
            return true;
        }

        return false;
    }
}
```

### Validation

[](#validation)

Error on validating associations

```
{
    "jsonapi": {
        "version": "1.0"
    },
    "errors": [
        {
            "detail": "Invalid value for this relation",
            "source": {
                "pointer": "/data/relationships/authors",
                "parameter": "1"
            }
        }
    ]
}
```

Validate attributes if you have defined validators on entities.

```
{
    "jsonapi": {
        "version": "1.0"
    },
    "errors": [
        {
            "detail": "This value is too short. It should have 3 characters or more.",
            "source": {
                "pointer": "/data/attributes/name",
                "parameter": "h"
            }
        }
    ]
}
```

### Error handler

[](#error-handler)

All errors such as:

- Internal server error (500)
- Not found (404)
- Access denied (403)

has responses like this:

```
{
    "meta": {
        "code": 0,
        "message": "No route found for \"GET /book\"",
        "file": "/var/www/vendor/symfony/http-kernel/EventListener/RouterListener.php",
        "line": 139,
        "trace": [
            {
                "file": "/var/www/vendor/symfony/event-dispatcher/EventDispatcher.php",
                "line": 212,
                "function": "onKernelRequest"
            },
            {
                "file": "/var/www/vendor/symfony/event-dispatcher/EventDispatcher.php",
                "line": 44,
                "function": "doDispatch"
            },
            {
                "file": "/var/www/vendor/symfony/http-kernel/HttpKernel.php",
                "line": 125,
                "function": "dispatch"
            },
            {
                "file": "/var/www/vendor/symfony/http-kernel/HttpKernel.php",
                "line": 66,
                "function": "handleRaw"
            },
            {
                "file": "/var/www/vendor/symfony/http-kernel/Kernel.php",
                "line": 188,
                "function": "handle"
            },
            {
                "file": "/var/www/public/index.php",
                "line": 37,
                "function": "handle"
            }
        ]
    },
    "links": {
        "self": "/book"
    },
    "errors": [
        {
            "status": "404",
            "code": "NO_ROUTE_FOUND_FOR_\"GET_/BOOK\"",
            "title": "No route found for \"GET /book\""
        }
    ]
}
```

NOTICE: the "meta" field gets filled just on development environment.

Configuration
-------------

[](#configuration)

You can configure this bundle with following options:

```
#config/packages/json_api.yaml

json_api:
    documentationSchema: 'openapi'
    controllerNamespace: 'Controller'
```

Supported documentation schemas are `openapi` and `swagger`. If you want to generate controllers in a different namespace, for example `Controller\Api` you can use controllerNamespace configuration option. To prefix Api routes you can use Symfony routes configuration:

```
#config/routes/annotations.yaml

api:
    resource: ../../src/Controller/Api
    type: annotation
    prefix: /api
```

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community29

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 72.2% 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 ~39 days

Recently: every ~121 days

Total

31

Last Release

1624d ago

Major Versions

v3.0.0 → v4.0.02020-02-09

v3.1.0 → v4.1.02020-03-07

v3.1.1 → v4.1.12020-03-07

v3.1.2 → v4.1.22020-03-15

v4.3.1 → v5.0.02021-12-06

PHP version history (4 changes)v1.0.0PHP ^7.2

v1.2.0PHP ^7.1

v4.3.0PHP ^7.1.0||^8.0.0

v5.0.0PHP &gt;=7.1.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/141dbaaae0e92a856319b037788d8a6e04f4fbcf095950444548e3e7bec55789?d=identicon)[paknahad](/maintainers/paknahad)

---

Top Contributors

[![paknahad](https://avatars.githubusercontent.com/u/1970573?v=4)](https://github.com/paknahad "paknahad (83 commits)")[![mpaden3](https://avatars.githubusercontent.com/u/20721839?v=4)](https://github.com/mpaden3 "mpaden3 (14 commits)")[![AlexLisenkov](https://avatars.githubusercontent.com/u/3088831?v=4)](https://github.com/AlexLisenkov "AlexLisenkov (5 commits)")[![Krilo89](https://avatars.githubusercontent.com/u/5180445?v=4)](https://github.com/Krilo89 "Krilo89 (3 commits)")[![mnugter](https://avatars.githubusercontent.com/u/1712411?v=4)](https://github.com/mnugter "mnugter (2 commits)")[![marojeee](https://avatars.githubusercontent.com/u/50014029?v=4)](https://github.com/marojeee "marojeee (1 commits)")[![hcpss-banderson](https://avatars.githubusercontent.com/u/10929134?v=4)](https://github.com/hcpss-banderson "hcpss-banderson (1 commits)")[![mohamed-akef](https://avatars.githubusercontent.com/u/1524321?v=4)](https://github.com/mohamed-akef "mohamed-akef (1 commits)")[![brunozoric](https://avatars.githubusercontent.com/u/10399339?v=4)](https://github.com/brunozoric "brunozoric (1 commits)")[![nelson6e65](https://avatars.githubusercontent.com/u/9272498?v=4)](https://github.com/nelson6e65 "nelson6e65 (1 commits)")[![b1rdex](https://avatars.githubusercontent.com/u/312855?v=4)](https://github.com/b1rdex "b1rdex (1 commits)")[![steveclifton](https://avatars.githubusercontent.com/u/14119296?v=4)](https://github.com/steveclifton "steveclifton (1 commits)")[![taccommandeur](https://avatars.githubusercontent.com/u/34679042?v=4)](https://github.com/taccommandeur "taccommandeur (1 commits)")

---

Tags

generatorjsonapijsonapi-serversymfony-bundlephpsymfonygeneratorcode generatorrestfulJSON-API

###  Code Quality

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/paknahad-jsonapi-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/paknahad-jsonapi-bundle/health.svg)](https://phpackages.com/packages/paknahad-jsonapi-bundle)
```

###  Alternatives

[sylius/sylius

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

8.4k5.6M651](/packages/sylius-sylius)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)[openai-php/symfony

Symfony Bundle for OpenAI

215715.5k3](/packages/openai-php-symfony)

PHPackages © 2026

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