Extend Magento REST API in custom module

后端 未结 1 2025
伪装坚强ぢ
伪装坚强ぢ 2021-02-10 18:35

I want to use Magento Rest-Api to add custom data into Magento table. I have added one table into Magento db and created module with Rest API for that using following link

相关标签:
1条回答
  • 2021-02-10 19:17

    magento\app\code\core\Mage\Api2\Model\Resource.php is only allowed collection action type for create method.. so changed in api2.xml file and setup required fields in attribute tag

    magento\app\code\community\MagePim\Extapi\etc\api2.xml

    <?xml version="1.0"?>
    <config>
        <api2>
            <resource_groups>
                <extapi translate="title" module="api2">
                    <title>Custom API calls</title>
                    <children>
                        <extapi translate="title" module="api2">
                            <title>My Api</title>
                        </extapi>
                    </children>
                </extapi>
            </resource_groups>
            <resources>
                <extapi translate="title" module="api2">
                    <group>extapi</group>
                    <model>extapi/api2</model>
                    <working_model>extapi/api2</working_model>
                    <title>Custom Api</title>
                    <privileges>
                        <admin>
                            <create>1</create>
                            <retrieve>1</retrieve>
                            <update>1</update>
                            <delete>1</delete>
                        </admin>
                    </privileges>
                    <attributes>
                        <owner_id>Owner ID</owner_id>
                        <identityid>Identity ID</identityid>
                        <social_id>Social ID</social_id>
                        <status>Status</status>
                        <text>Text</text>
                        <request_timestamp>Request Time</request_timestamp>
                        <status_timestamp>Status Time</status_timestamp>
                    </attributes>
                    <routes>
                        <!-- Call For V1.php _retrieve() -->
                        <route_entity>
                            <route>/scheduler</route>
                            <action_type>entity</action_type>
                        </route_entity>
                        <!-- Call For V1.php _create() -->
                        <route_collection>
                            <route>/scheduler/create</route>
                            <action_type>collection</action_type>
                        </route_collection>
                    </routes>
                    <versions>1</versions>
                </extapi>
            </resources>
        </api2>
    </config>
    

    magento\app\code\community\MagePim\Extapi\Model\Api2\Rest\Admin\V1.php

    /**
     * Override for Magento's REST API
     */
    class Magepim_Extapi_Model_Api2_Rest_Admin_V1 extends Mage_Api2_Model_Resource {
    
        protected function _retrieve(){
            return json_encode($shedulerData);
        }
        protected function _create($shedulerData){
            return json_encode($shedulerData);
        }
        protected function _retrieveCollection(){
            return json_encode(array('method'=>'_retrieveCollection'));
        }
    ....................
    }
    
    0 讨论(0)
提交回复
热议问题