bentools/api-platform-create-resource

Allows creating resources through PUT operations.

0.1.0 2020-01-31 15:04 UTC

This package is auto-updated.

Last update: 2024-03-08 04:09:44 UTC


README

This bundle is an add-on to API Platform that brings resource creation through the PUT verb.

Kind of upsert in REST.

This means you can send the following request:

PUT /api/books/d9a5551f-f188-45e4-a034-00b744a08a31
Content-Type: application/ld+json
Accept: application/ld+json

{"name":  "Book title"}

If /api/books/d9a5551f-f188-45e4-a034-00b744a08a31 doesn't exist, this resource will be created, otherwise its content will be replaced; as of the specification.

This way, your client doesn't have to care whether or not it should POST or PUT, it always PUTs. This involves that you delegate the resource ID generation to the client side (which may be done with UUIDs / ULIDs for example).

Installation

composer require bentools/api-platform-create-resource:0.1.*

Important notice: This bundle must be loaded before ApiPlatformBundle to work properly.

Configuration

Resource classes which expect this behavior must be explicitely listed:

# config/packages/api_platform_create_resource.yaml
api_platform_create_resource:
  allowed_classes:
    App\Entity\Book: ~

Resource instanciation

If your entity/object needs a factory service to instanciate it, you can implement your own BenTools\ApiPlatform\CreateResource\Factory\ItemFactoryInterface and reference it as a service:

# config/packages/api_platform_create_resource.yaml
api_platform_create_resource:
  allowed_classes:
    App\Entity\Book: '@App\Services\BookFactory'

Tests

Who cares? 😄

License

MIT.