Magento – How to set a method in route action_type in rest API (api2.xml)

amazon-web-servicesapiapi-filtermagento-1.9rest

My route section in api2.xml

 <routes>
                <route_collection>
                    <route>/mymodule/user</route>
                    <action_type>collection</action_type>
                </route_collection>  
                <route_create>
                    <route>/mymodule/user/create</route>
                    <action_type>create</action_type>
                </route_create>     
                <route_update>
                    <route>/mymodule/user/update/:id</route>
                    <action_type>update</action_type>
                </route_update>               
            </routes>

My model methods: mymodule/Model/Api2/User/Rest/Guest/V1.php

I extend Mage_Api2_Model_Resource

public function _retrieve() {
    return json_encode(array(0=>'retrieve Function Call done')); 
}

//Collection
public function _retrieveCollection() {
      //Some codes 
   return json_encode(array(0=>'retrieveCollection Function Call done')); 
}

//Create
public function _retrieveCreate() {
     //Some codes
     return json_encode(array(0=>'retrieveCreate Function Call done')); 
}

//Update
public function _retrieveUpdate) {
     //Some codes
     return json_encode(array(0=>'retrieveUpdate Function Call done'));
}

I get an error message on API call magento-host/api/rest/mymodule/user/create "code": 405," message": "Resource method not implemented yet."

How can I resolve this?

Best Answer

As far as I understand you can only have a route_entity and a route_collection and with those you can have a GET, PUT, POST and DELETE (have a look at http://www.restapitutorial.com/lessons/httpmethods.html) and they will match functions called _retrieve, _update, _create and _delete. To implement your own REST API methods have a good look at e.g. api2.xml in the customer module. So for instance in your own api2.xml file you can have:

<privileges>
   <admin>
     <create>1<create>
     <update>1</update>
     <retrieve>1</retrieve>
   </admin>
   ....
   <routes>
     <route>/myextension/myproduct/:product_id</route>
        <action_type>collection</action_type>
     </route>

Note that I use admin here because a Guest can use only GET. My admin role can now POST (create) and PUT (update) and GET (retrieve).

My code would then look like this:

class Mymodule_Myextension_Model_Api2_Myproduct_Rest_Admin_v1 extends Mymodule_Myextension_Model_Api2_Myproduct
{
protected function _update($productData)
{ ... }
protected function _create($productData)
{ ... }
protected function _retrieve($productData)
{ ... }

There is a good tutorial on http://devdocs.magento.com/guides/m1x/other/ht_extend_magento_rest_api.html

Related Topic