1

I am building an API with Apigility. I want to allow POST to a Collection (for creating a new unnamed entity) and also to an Entity (for creating a new named entity).

Apigility's Resource class, 'create' method is invoked on POST to either the Collection or an Entity, and receives only a 'data' parameter which is the POSTed payload. There is no 'id' parameter because the method is usually invoked only for Collections.

If invoked on an Entity - i.e. if an 'id' value was provided in the URI - how can I know the ID value that was provided?

Thanks

vanwinter
  • 153
  • 1
  • 8

1 Answers1

1

YES! I figured it out:

public function create($data)
{
    return array('id' => 
        $this->getEvent()->getRouteMatch()->getParam('dataset_id'));
}

POST http://localhost/dataset/xyz123

(response) =>

{
    "id": "xyz123",
    ...
}

POST http://localhost/dataset

(response) =>

{
    "id": null,
    ...
}
vanwinter
  • 153
  • 1
  • 8