0

I seem to only be able to create new entities when calling /merchant/1, however /merchant will return a 405 status.

This is my resource method for POSTs:

public function create($data)
{
     return $this->mapper->create($data);
}

The id for this entity is an auto_incrment field so for me it makes sense for the client not to provide an identifier.

here's a snippet from my module.config.php:

'zf-rest' => array(
    'MyTest\\V1\\Rest\\Merchant\\Controller' => array(
        'listener' => 'MyTest\\V1\\Rest\\Merchant\\MerchantResource',
        'route_name' => 'MyTest.rest.merchant',
        'route_identifier_name' => 'merchant_id',
        'collection_name' => 'merchant',
        'entity_http_methods' => array(
            0 => 'GET',
            1 => 'PATCH',
            2 => 'PUT',
            3 => 'POST',
        ),
        'collection_http_methods' => array(
            0 => 'GET',
        ),
        'collection_query_whitelist' => array(),
        'page_size' => 25,
        'page_size_param' => null,
        'entity_class' => 'MyTest\\V1\\Rest\\Merchant\\MerchantEntity',
        'collection_class' => 'MyTest\\V1\\Rest\\Merchant\\MerchantCollection',
        'service_name' => 'Merchant',
    ),

Not sure what else I can provide to help you guys understand the situation but happy to provide more details on request.

Thanks for your time.

Jason
  • 507
  • 3
  • 10

2 Answers2

2

So I added POST as an allowed HTTP Collection Method and it now works for posting individual entities.

Not sure if that's by design or not but solved my problem.

Jason
  • 507
  • 3
  • 10
  • 1
    POST should only be allowed on Collection URI's... why? it doesn't make sense to Apigility to POST a new entity on an already existing one, nor does it make sense to POST to a non-existing URI.. not sure why your setup changed. – Erik Jul 01 '14 at 19:27
  • I think it was more the term "collection" which threw me off. I wanted to only be able to post a single new entity, not x number of them. The whole lack of identifier in the URL is what made me try using the collection URL in the end so looking at things that way, it makes more sense now. – Jason Jul 02 '14 at 06:45
2

If you create a new resource on a Restful API you do a post on the collection route. So you should add your POST method to the collection_http_methods array. This is totally according to Restful specifications.

'collection_http_methods' => array(
    0 => 'GET',
    1 => 'POST',
),

I think if you change that it should work. One more thing, I don't know why they do it in the documentation like that, but my http_methods arrays look like this:

'collection_http_methods' => array('GET', 'POST')

Much easier if you ask me :)

Wilt
  • 41,477
  • 12
  • 152
  • 203