Pus*_*dra 6 php api rest magento
我需要休息api来创建磁铁客户,因为我遵循了这个教程http://www.authenticdesign.co.uk/extending-magento-rest-api-v2/
我一步一步地跟着它,但是当我在休息客户端测试api时,它给了我: {"messages":{"error":[{"code":404,"message":"Request does not match any route."}]}}
我不知道我在哪里弄错了.帮帮我,因为我对magento和php都很新.
步骤是:
1.在(app/etc/module/Custom_Restapi.xml)启用扩展
<config>
<modules>
<Custom_Restapi>
<active>true</active>
<codePool>local</codePool>
</Custom_Restapi_Groups>
</modules>
</config>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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
{
}
?>
Run Code Online (Sandbox Code Playgroud)
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"));
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我的网址就像:baseurl/api/rest/customer
我会把它放在评论中,因为我觉得这不是一个完全完整的答案,但我还没有被允许.一些东西:
config.xml中的全局标记未关闭.
您无法使用引用实体的URL创建记录,您必须使用api2.xml中route_collection节点中定义的收集路由.所以你应该打电话给/ api/rest/customer.
由于该方法是通过http方法(post/get/delete/etc)和正文内容选择的,因此不需要单独的"创建"路由.我会为route_entity元素推荐一条"customer /:id"的路由.所以也要确保你提交的是HTTP POST.
我无法重现您发布的确切错误,但在纠正上述项目后我能够正常工作.
此外,请确保在管理区域中授予此资源的权限,并清除Web服务配置缓存.
您列出的特定异常将在路由方法的Mage_Api2_Model_Router中引发.
我重写了这个并使用工作模块在github上创建了一个repo:https://github.com/themizzi/Custom-Magento-Rest-Api2.该模块使用访客访问权限,因为我没有时间完成整个oAuth交易,但如果您只是将api2.xml中的访客节点更新为admin并更新您在管理区域中的访问权限,那么它将起作用.