Marceli Po
Marceli Po

Reputation: 743

Zend Framework 2: How can I add in RESTful API a custom http method?

I'm trying to create a custom http method in RESTful API. I was reading the documentation and it is said that you can do it buy adding a simple action in controller and then for example conifg your route with child routes with action => action_name but in the code I have spotted addHttpMethodHandler() method in Zend\Mvc\Controller\AbstractRestfulController.php so in controller construct method I have added:

$add = function () {
        return new JsonModel(array(
                'id' => 2222,
        ));
    };

    $this->addHttpMethodHandler('someAction', $add);
    var_dump($this->customHttpMethodsMap);

With the var_dump I can see that this new function is added but I just wonder how can I call it or maybe I'm missing the point.

Regards,

Upvotes: 0

Views: 2604

Answers (1)

Rick B
Rick B

Reputation: 21

I actually wrote a blog post on this because I had so much trouble too.

The problem is that in addition to calling addHttpMethodHandler within the abstract restful controller, you also need to make sure that the Zend Request class knows that your http method exists.

Here is a link to a better explanation: http://richardbrock1.wordpress.com/2013/03/23/custom-http-methods-in-zf2/

Upvotes: 2

Related Questions