我一步一步地跟着它,但是当我在休息客户端测试api时,它给了我:{“messages”:{“error”:[{“code”:404,“message”:“请求与任何路由都不匹配. “}]}}
我不知道我在哪里弄错了.帮帮我,因为我对magento和PHP都很新.
步骤是:
1.在(app / etc / module / Custom_Restapi.xml)启用扩展
<config> <modules> <Custom_Restapi> <active>true</active> <codePool>local</codePool> </Custom_Restapi_Groups> </modules> </config>
2. config.xml at(app / code / local / Custom / Restapi / etc / config.xml)
<?xml version="1.0"?> <config> <modules> <Custom_Restapi> <version>0.1.0.0</version> </Custom_Restapi> </modules> <global> <models> <restapi> <class>Custom_Restapi_Model</class> </restapi> </models> </global> </config>
3. api2.xml at(app / code / local / Custom / Restapi / etc / api2.xml)
<?xml version="1.0"?> <config> <api2> <resource_groups> <restapi translate="title" module="Custom_Restapi"> <title>Custom Rest API</title> <sort_order>10</sort_order> </restapi> </resource_groups> <resources> <restapi translate="title" module="Custom_Restapi"> <group>restapi</group> <model>restapi/api2_restapi</model> <title>Testing My Rest API</title> <sort_order>10</sort_order> <privileges> <admin> <create>1</create> </admin> </privileges> <attributes translate="" module="Custom_Restapi"> <firstname>First Name</firstname> <lastname>Last Name</lastname> <email>Email</email> <password>Password</password> </attributes> <routes> <route> <route>/customer</route> <action_type>collection</action_type> </route> </routes> <versions>1</versions> </restapi> </resources> </api2> </config>
4.模型类Restapi.PHP at(app / code / local / Custom / Restapi / Model / Api2 / Restapi.PHP)
<?PHP class Custom_Restapi_Model_Api2_Restapi extends Mage_Api2_Model_Resource { } ?>
5. V1.PHP at(app / code / local / Custom / Restapi / Model / Api2 / Restapi / Rest / Admin / V1.PHP)
<?PHP class Custom_Restapi_Model_Api2_Restapi_Rest_Admin_V1 extends Custom_Restapi_Model_Api2_Restapi { /** * Create a customer * @return array */ public function _create() { $requestData = $this->getRequest()->getBodyParams(); $firstName = $requestData['firstname']; $lastName = $requestData['lastname']; $email = $requestData['email']; $password = $requestData['password']; $customer = Mage::getModel("customer/customer"); $customer->setFirstname($firstName); $customer->setLastname($lastName); $customer->setEmail($email); $customer->setPasswordHash(md5($password)); $customer->save(); return json_encode(array("testing","Success")); } } ?>
我的网址就像:baseurl / api / rest / customer
> config.xml中的全局标记未关闭.
>您无法使用引用实体的网址创建记录
必须使用route_collection中定义的收集路由
api2.xml中的节点.所以你应该打电话给/ api / rest / customer.
>自该方法以来,无需单独的“创建”路径
由http方法(post / get / delete / etc)和正文选择
内容.我会推荐一条“customer /:id”的路线
route_entity元素.所以也要确保你提交的是HTTP POST.
我无法重现您发布的确切错误,但在纠正上述项目后我能够正常工作.
此外,请确保在管理区域中授予此资源的权限,并清除Web服务配置缓存.
您列出的特定异常将在路由方法的Mage_Api2_Model_Router中引发.
我重写了这个并在github上创建了一个带有工作模块的repo:https://github.com/themizzi/Custom-Magento-Rest-Api2.该模块使用Guest访问权限,因为我没有时间完成整个oAuth交易,但是如果您只是将api2.xml中的guest虚拟机节点更新为admin并在管理区域更新您的访问权限,它将工作.