简体   繁体   English

适应性-POST到实体-如何获取ID值?

[英]Apigility - POST to Entity - How do I get the ID value?

I am building an API with Apigility. 我正在使用Apigility构建API。 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). 我想允许POST到一个集合(创建一个新的未命名的实体), 将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. Apigility的Resource类“ create”方法在POST上调用到集合或实体,并且仅接收“ data”参数,即POSTed的有效负载。 There is no 'id' parameter because the method is usually invoked only for Collections. 没有“ id”参数,因为该方法通常仅对Collections调用。

If invoked on an Entity - ie if an 'id' value was provided in the URI - how can I know the ID value that was provided? 如果在实体上调用(即,如果在URI中提供了“ id”值),我怎么知道所提供的ID值?

Thanks 谢谢

YES! 是! I figured it out: 我想到了:

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

POST http://localhost/dataset/xyz123 POST http:// localhost / dataset / xyz123

(response) => (响应)=>

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

POST http://localhost/dataset POST http:// localhost / dataset

(response) => (响应)=>

{
    "id": null,
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM