Magento rest api getting error请求不匹配任何路由

时间:2016-04-19 14:47:56

标签: rest magento

我已经创建了一个rest api web服务但是我在点击url时遇到错误

http://localhost:81/magento/api/rest/category/2

<magento_api>
    <messages>
        <error>
            <data_item>
                <code>404</code>
                <message>Request does not match any route.</message>
            </data_item>
        </error>
    </messages>
</magento_api>

我的api2.xml是: -

<?xml version="1.0"?>
<config>
    <api2>
        <resource_groups>
            <esoft_rescategories translate="title" module="Esoft_Restcategories">
                <title>Esoft Restcategories API</title>
                <sort_order>11</sort_order>
            </esoft_rescategories>
        </resource_groups>
        <resources>
            <esoft_restcategories translate="title" module="Esoft_Restcategories">
                <group>esoft_rescategories</group>
                <model>esoft_restcategories/api2_restapi</model>
                <title>Categories</title>
                <sort_order>11</sort_order>
                <privileges>
                    <admin>
                        <create>1</create>
                        <!--<retrieve>1</retrieve>
                        <update>1</update>
                        <delete>1</delete>-->
                    </admin>
                    <guest>
                        <retrieve>1</retrieve>
                        <!--<create>1</create>
                        <update>1</update>
                        <delete>1</delete>-->
                    </guest>
                </privileges>
                 <attributes>
                <category_id>Category ID</category_id>
                <name>Name</name>
                <parent_id>Category Parent ID</parent_id>
                <child_id>Category Child List</child_id>
                <active>Active</active>
                <level>Level</level>
                <position>Position</position>
            </attributes>
             <routes>
               <route_entity>
                        <route>/categories/:cat_id</route>
                        <action_type>entity</action_type>
                    </route_entity>
                    <route_collection>
                        <route>/categories</route>
                        <action_type>collection</action_type>
                    </route_collection>
            </routes>
                <versions>1</versions>
            </esoft_restcategories>
        </resources>
    </api2>
</config>

我的客人版本文件是:

<?php

class Esoft_Restcategories_Model_Api2_Restapi_Rest_Guest_V1 extends Esoft_Restapi_Model_Api2_Restapi {

  /**
 * Retrieve list of category list.
 *
 * @return array
 */
protected function _retrieveCollection()
{   
    $ruleId = $this->getRequest()->getParam('cat_id');

//  $cat_mod = Mage::getModel('catalog/category')->load($ruleId)->toArray();
    $cats = Mage::getModel('catalog/category')->load($ruleId);
    $subcats  = Mage::getModel('catalog/category')->load($ruleId)->getChildren();

    $cur_category = array();

    $node['category_id'] = $ruleId;
    $node['name'] = $cats->getName();
    $node['parent_id'] = $cats->getParentId();
    $node['child_id'] = $subcats;
    if($cats->getIsActive()){
        $node['active'] = 1;
    }else{
        $node['active'] = 0;
    }
    $node['level'] = $cats->getLevel();
    $node['position'] = $cats->getPosition();

    $cur_category[] = $node;

//  $subcats  = Mage::getModel('catalog/category')->load($ruleId)->getAllChildren();
//  $subcats  = Mage::getModel('catalog/category')->load($ruleId)->getChildren();

    if($subcats != '') 
    {
        foreach(explode(',',$subcats) as $subCatid)
        {
            $_category = Mage::getModel('catalog/category')->load($subCatid);
            $childcats  = Mage::getModel('catalog/category')->load($subCatid)->getChildren();

            $node['category_id'] = $subCatid;
            $node['name'] = $_category->getName();
            $node['parent_id'] = $_category->getParentId();
            $node['child_id'] = $childcats;
            if($_category->getIsActive()){
                $node['active'] = 1;
            }else{
                $node['active'] = 0;
            }
            $node['level'] = $_category->getLevel();
            $node['position'] = $_category->getPosition();

            $cur_category[] = $node;

        }
    }

    return $cur_category;       
}
}

请告诉我如何解决此错误。

另请告诉我们我们定义路线的依据。

2 个答案:

答案 0 :(得分:1)

  

根据您的说明,您的网址格式错误(路线未命中匹配给定网址)。

<route>/categories/:cat_id</route>
        ^^^^^^^^^

所以你需要改变下面的网址

http://localhost:81/magento/api/rest/categories/2

答案 1 :(得分:0)

对于寻找此错误原因的用户的简单答案:

  

请求与任何路由都不匹配

是您发布了错误的方法,例如GET / POST / PUT / DELETE。

也可能是API网址本身的路径错误。