PHPackages                             bentools/api-first-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. bentools/api-first-bundle

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

bentools/api-first-bundle
=========================

Provides classes for API-first designed projects with Symfony.

v0.6.2(9y ago)097MITPHPPHP &gt;=7

Since Jul 12Pushed 1y ago1 watchersCompare

[ Source](https://github.com/bpolaszek/apifirst-bundle)[ Packagist](https://packagist.org/packages/bentools/api-first-bundle)[ RSS](/packages/bentools-api-first-bundle/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependencies (14)Versions (30)Used By (0)

APIFirst Bundle
===============

[](#apifirst-bundle)

Provides classes for API-first designed projects with Symfony.

For personnal purposes for the moment.

The goal of this bundle is to help in designing Symfony applications that can be consumed with an API and with an UI as well.

Important

This repository is no longer maintained and may be removed in a near future. You may consider creating a fork if you still require it.

Concept
-------

[](#concept)

A Symfony entity is a Resource that has an id. It should implement `ResourceInterface` which just requires implementing a `getId()` method.

Several classes interacts with this resource:

- ORM / ODM classes (EntityManager, Repository)
- Form classes
- Action classes (GET, POST, PUT, PATCH, DELETE, related resources, etc)

A `AbstractResourceHandler` is a service that gives access to the corresponding classes of a specific Resource.

**Here's the flow:**

- The `AbstractResourceHandler` provides form handling. It is HTTP agnostic: you can submit a form from a `Request` or from raw data (array). You can use it in cron jobs, bulk actions, its role is not to send a `Response` but a `Resource`. When the form fails it throws a `ValidationFormException`.
- The Action classes calls the `AbstractResourceHandler` to transform a `Resource` with a `Request`.
- The Action classes can generate a pre-response, in which they can define:
    - What to do on success (redirect to an URL, add flashes for instance, in case of an UI request)
    - Which HTTP status code to reply (in case of an API submission)
- An event-listener will transform this `PreResponse` to the correct response with content-negociation (redirect + flash if the request came from an UI, status code in case of an API request)
- When a `ValidationFormException` is thrown from the `AbstractResourceHandler`, the Action class should:
    - Return a HTTP 200 response code with the form and the errors in case of an UI request
    - Return a HTTP 400 response code with the serialized form errors in case of an API request
    - The `BenTools\ApiFirstBundle\Model\AbstractCRUDAction::submitForm()` method will return the resolved callable `$success` on success; the Form object otherwise.

CSRF Protection
---------------

[](#csrf-protection)

- FOSRESTBundle can disable CSRF protection on a specific role.
- This is not the best solution since the same User can use both the API and the UI. This means if they have the ROLE\_API and logs in on the UI, they won't be CSRF-protected.

**APIFirstBundle** provides another solution:

- When you create your Form Types, don't extend `Symfony\Component\Form\AbstractType` but `BenTools\ApiFirstBundle\Form\ApiFirstAbstractType` instead
- Setup your form with automatic CSRF protection enabling:

```
    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver) {
        $resolver->setDefaults([
            'data_class'      => MyResource::class,
            'csrf_protection' => $this->shouldEnableCSRFProtection(),
        ]);
    }
```

- Declare your form type as a service since its constructor has a dependency on `api_first.api_consumer_detector`

Form Handling
-------------

[](#form-handling)

When you extend the `BenTools\ApiFirstBundle\Model\AbstractResourceHandler` class, you can call the `getCreationForm`, `getEditionForm` and the `getDeletionForm` methods.

If you're using an UI, it will create a *named* form. On the contrary, if you're posting data on the API, the keys won't be prefixed in the form.

For instance, if you're creating a new Contact resource with the UI, the app will expect the following form params:

```
[
    'contact' => [
        'firstname' => 'John',
        'lastname'  => 'Doe',
    ],
    '_token' => 'zef6rq1g6er8g1re6g81e6fertjh4yu6j4'
];

```

If you're using the API, the app will expect this:

```
[
    'firstname' => 'John',
    'lastname'  => 'Doe',
];

```

Of course we could use non-named forms only. But this leads to issues with Symfony's *\_token* and *\_method* hidden fields that are misunderstood as forms *extra fields*.

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity64

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

Total

29

Last Release

3599d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/acdd1a8ee0e657ddd06cf11f98a32100ef7121afb8aa270a5b295f5c29c038b3?d=identicon)[bpolaszek](/maintainers/bpolaszek)

---

Top Contributors

[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (40 commits)")

### Embed Badge

![Health badge](/badges/bentools-api-first-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/bentools-api-first-bundle/health.svg)](https://phpackages.com/packages/bentools-api-first-bundle)
```

###  Alternatives

[sylius/sylius

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

8.5k5.9M719](/packages/sylius-sylius)[sulu/sulu

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

1.3k1.4M196](/packages/sulu-sulu)[kimai/kimai

Kimai - Time Tracking

4.8k8.7k1](/packages/kimai-kimai)[voryx/restgeneratorbundle

REST API Generator for Symfony 2

177142.7k1](/packages/voryx-restgeneratorbundle)

PHPackages © 2026

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