Search code examples
phpapizend-framework2laminas-api-tools

Apigility is requiring the route_identifier_name for Code-Connected REST POST Entity


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.


Solution

  • 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 :)