I am building RESTful API in Zend Framework 2. My route is article/person. I know that if id is not passed in url then it will call getList() method, not get().
In my case, I don't pass id as a get or post parameter but I pass it in HTTP header. As I use id to perform database operations, I want it to call get() method, not getList(). How can I tweak the code to do that?
Is it possible to specify exact method name to call in routing?
This does make your REST invalid, so it isn't actually REST anymore. You therefor cannot use the RestfulAbstractController without customization.
You can either write your own abstract controller or you override the
getIdentifiermethod:Make sure you set the correct identifier name in each controller. In this case, the identifier name should match the name of the header you are using.
Note this will be used for GET, PUT, PATCH, DELETE and HEAD requests, not only for GET!
/edit:
The
getIdentifiermethod is called in the flow a controller determines which method to run. Normally, it's this:dispatchis called (the controller is Dispatchable)dispatchtriggers an event "dispatch"onDispatchlistens to this eventFor #5, it checks for example if the request is a GET request. If so, it checks if there is an identifier given. If so, the
get()is used. If not, thegetList()is used. The "if there is an identifier given" check is done with thegetIdentifier()method.If you extend the AbstractRestfulController with your own abstract controller and override the
getIdentifier(), you can determine your own identifier. This way, you can check for the header instead of the route parameter or query parameter.