Skip to content

bpolaszek/api-platform-create-resource

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

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.

About

[Api-Platform] Allows creating resources through the PUT method.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages