phpzend-frameworklaminas-api-tools

Can't call a specific entity in Apigility


I can't call my endpoint in Apigility via GET. I have the following config:

'router' => [
    'routes' => [
        ...
        'test' => [
            'type' => Segment::class,
            'options' => [
                'route' => '/test[/:id]',
                'defaults' => [
                    'controller' => Resource\TestResource::class,
                ],
            ],
            'may_terminate' => true,
        ],
        ...
    ],
],
'zf-rest' => [
    Resource\TestResource::class => [
        'listener' => Resource\TestResource::class,
        'route_name' => 'api/rest/test',
        'route_identifier_name' => 'id',
        'entity_http_methods' => [
            'GET',
        ],
        'collection_http_methods' => [
        ],
    ],
],

And my resource looks like this:

class TestResource extends AbstractResourceListener
{
    public function fetch($id)
    {
        return ...
    }
}

Now I try to open the url /api/rest/test in my browser and get a 405 (method not allowed).


Solution

  • Now I try to open the url /api/rest/test in my browser and get a 405 (method not allowed).

    Your URL is actually calling a collection. In order to call an entity, you need to specify an entity id, i.e.: /api/rest/test/1 For as long as there's not ID at the end of the URL, Apigility will match the collection route and according to your config, there's absolutely no http method allowed for collection.