您的位置:首页 > 其它

magento中自己的模块使用 REST API (一)

2015-04-16 15:55 267 查看
最近公司开发要用到自带的REST API,用的是系统自带的功能

看了下官网的介绍以及catalog中product的写法,依葫芦画瓢,然后自己尝试,得出REST API的基本流程

第一次写博客,写的不好请见谅。

-------------------------------------正文分割线---------------------------------

首先在自己的模块(我的模块为o2o)     etc中建立api2.xml

<?xml version="1.0"?>
<config>
<api2>
<resource_groups>
<eyepp translate="title" module="api2">
<title>Eyepp</title>
<sort_order>31</sort_order>
<children>
<eyepp_o2o translate="title" module="api2">
<title>Eyepp O2O</title>
<sort_order>51</sort_order>
</eyepp_o2o>
</children>
</eyepp>
</resource_groups>
<resources>
<shop translate="title" module="api2">
<group>eyepp_o2o</group>
<model>eyepp_o2o/api2_shop</model>
<working_model>eyepp_o2o/shop</working_model>
<title>Eyepp Shop</title>
<sort_order>10</sort_order>
<!--  Rest-Roles  -->
<privileges>
<admin>
<create>1</create>
<retrieve>1</retrieve>
<update>1</update>
<delete>1</delete>
</admin>
<customer>
<retrieve>1</retrieve>
<update>1</update>
<delete>1</delete>
</customer>
<guest>
<retrieve>1</retrieve>
</guest>
</privileges>
<attributes translate="entity_id url" module="api2">
<entity_id>Shop ID</entity_id>
<url>Url</url>
</attributes>
<entity_only_attributes>
<!--   REST-Attributes 中属性带* ,即必要的   -->
<customer>
<read>
<shop_name>1</shop_name>
</read>
</customer>
<guest>
<read>
<shop_name>1</shop_name>
</read>
</guest>
</entity_only_attributes>
<routes>
<route_entity>
<route>/shops/:id</route>
<action_type>entity</action_type>
</route_entity>
<route_collection>
<route>/shops</route>
<action_type>collection</action_type>
</route_collection>
</routes>
<versions>1</versions>
</shop>
<shop_order>
<!-- 其他的。。。。-->
</shop_order>
</resources>
</api2>
</config>
这其中<resource_groups>没什么说的,就是建立一个组,自定义命名,然后在下面的<group>eyepp_o2o</group>就是说将<shop>这个资源放到eyepp_o2o下

<privileges>中是后台REST-Roles中权限列表,

<attributes>是后台REST-Atttibutes中的权限列表,<entity_only_attributes>表示那些带*的属性,<exclude_attributes>表示不出现的属性,根据自己的需求写,这个地方我就没写了

<routes>路由,<route_entity>表示其为单个实体, <route_collection>则指的一个集,比较容易理解

<model>eyepp_o2o/api2_shop</model>这个很明显,这是一个model,magento会实例化。

此时应建立一个php文件,在模块的Model文件夹下,新建Api2文件夹,然后在其文件夹下建立Shop.php



<?php
class Eyepp_O2o_Model_Api2_Shop extends Mage_Api2_Model_Resource{}
可以不用写方法,因为父类里有相应的方法,也可以根据自己的需求重写方法。

一般会在这里重写public function getAvailableAttributes($userType, $operation) {}

这个方法是用来控制在后台REST-Attributes中的属性显示,可以参考Mage_Catalog_Model_Api2_Product中是怎么重写的

<working_model>eyepp_o2o/shop</working_model>表示其实体,此处Eyepp_O2o_Model_Shop应该已经创建过了

此时后台REST-Roles和REST-Attributes下应该能看见权限分配了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rest magento api