Magento 中的自定义休息 Api
Custom Rest Api in Magento
我需要休息 api 才能在 magneto 中创建客户,为此我遵循了本教程 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>
2。 config.xml 在 (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 在 (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.模型 Class Restapi.php 在 (app/code/local/Custom/Restapi/Model/Api2/Restapi.php)
<?php
class Custom_Restapi_Model_Api2_Restapi extends Mage_Api2_Model_Resource
{
}
?>
5. V1.php 在 (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"));
}
}
?>
而我的 url 就像:baseurl/api/rest/customer
我会把它放在评论中,因为我觉得这不是一个完全完整的答案,但我还没有被允许这样做。一些事情:
您在 config.xml 中的全局标记未关闭。
您不能使用引用实体的 url 创建记录,您
必须使用 route_collection 中定义的收集路线
api2.xml 中的节点。所以你应该打电话给 /api/rest/customer.
不需要单独的"create"路由,因为方法
由 http 方法 (post/get/delete/etc) 和正文选择
内容。我会推荐 "customer/:id" 的路线
route_entity 元素。因此,还要确保您提交的是 HTTP POST.
我无法重现您发布的确切错误,但在更正上述项目后我能够使它正常工作。
此外,请务必在管理区域授予对此资源的权限并清除您的 Web 服务配置缓存。
你列出的特定异常在路由方法中Mage_Api2_Model_Router抛出。
我修改了它并在 github 上使用工作模块创建了一个存储库:https://github.com/themizzi/Custom-Magento-Rest-Api2。该模块使用来宾访问权限,因为我没有时间完成整个 oAuth 交易,但如果您只需将 api2.xml 中的来宾节点更新为管理员并在管理区域更新您的访问权限,它就会工作。
首先你在
中犯了一个小错误
步骤 1. 在 (app/etc/module/Custom_Restapi.xml)
启用扩展
您以 <Custom_Restapi>
的身份打开标签,但以 <Custom_Restapi>
的身份关闭标签
<Custom_Restapi_Grops>
而且你错过了 <?xml version="1.0"?>
标签.
其次,您可以将代码放在 _retrieveCollection()
中,如
api2.xml 您只定义了 1 条路线 & 用于检索集合。
将您的代码放在 _retrieveCollection()
或
_retrieveCollection()
调用您的 _create
方法。
您最后定义了 名字、姓氏、电子邮件 &
password 作为 api2.xml 中的属性它们不是 POST 参数,而且我要么不熟悉
getBodyParams() 方法
要么您必须定义路由以通过 api2.xml 中的 URL 获取所有 4 个参数,要么
您可以通过在查询字符串中附加所有参数来尝试 $_GET[]。
希望对你有所帮助。
谢谢
我需要休息 api 才能在 magneto 中创建客户,为此我遵循了本教程 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>
2。 config.xml 在 (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 在 (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.模型 Class Restapi.php 在 (app/code/local/Custom/Restapi/Model/Api2/Restapi.php)
<?php
class Custom_Restapi_Model_Api2_Restapi extends Mage_Api2_Model_Resource
{
}
?>
5. V1.php 在 (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"));
}
}
?>
而我的 url 就像:baseurl/api/rest/customer
我会把它放在评论中,因为我觉得这不是一个完全完整的答案,但我还没有被允许这样做。一些事情:
您在 config.xml 中的全局标记未关闭。
您不能使用引用实体的 url 创建记录,您 必须使用 route_collection 中定义的收集路线 api2.xml 中的节点。所以你应该打电话给 /api/rest/customer.
不需要单独的"create"路由,因为方法 由 http 方法 (post/get/delete/etc) 和正文选择 内容。我会推荐 "customer/:id" 的路线 route_entity 元素。因此,还要确保您提交的是 HTTP POST.
我无法重现您发布的确切错误,但在更正上述项目后我能够使它正常工作。
此外,请务必在管理区域授予对此资源的权限并清除您的 Web 服务配置缓存。
你列出的特定异常在路由方法中Mage_Api2_Model_Router抛出。
我修改了它并在 github 上使用工作模块创建了一个存储库:https://github.com/themizzi/Custom-Magento-Rest-Api2。该模块使用来宾访问权限,因为我没有时间完成整个 oAuth 交易,但如果您只需将 api2.xml 中的来宾节点更新为管理员并在管理区域更新您的访问权限,它就会工作。
首先你在
中犯了一个小错误步骤 1. 在 (app/etc/module/Custom_Restapi.xml)
启用扩展您以
<Custom_Restapi>
的身份打开标签,但以<Custom_Restapi>
的身份关闭标签<Custom_Restapi_Grops>
而且你错过了<?xml version="1.0"?>
标签.其次,您可以将代码放在
_retrieveCollection()
中,如 api2.xml 您只定义了 1 条路线 & 用于检索集合。将您的代码放在
_retrieveCollection()
或_retrieveCollection()
调用您的_create
方法。您最后定义了 名字、姓氏、电子邮件 & password 作为 api2.xml 中的属性它们不是 POST 参数,而且我要么不熟悉 getBodyParams() 方法
要么您必须定义路由以通过 api2.xml 中的 URL 获取所有 4 个参数,要么 您可以通过在查询字符串中附加所有参数来尝试 $_GET[]。
希望对你有所帮助。
谢谢