【问题标题】:How to pass multiple parameters in magento rest api2如何在magento rest api2中传递多个参数
【发布时间】:2016-08-10 11:53:31
【问题描述】:

除了我们将在 api2.xml 中提到的默认参数之外,如何在 magento rest api2 中传递多个参数

<routes>
        <route_collection>
            <route>/appointmentupdate/:appointmentid</route>
            <action_type>collection</action_type>
        </route_collection>
</routes>

【问题讨论】:

    标签: api rest magento


    【解决方案1】:

    我在另一个 api2 magento 调用中发现了这个

    app\code\core\Mage\Catalog\etc\api2.xml:
      187:                         <route>/products/:id/store/:store</route>
    

    所以 url 调用将是:

    产品/[id]/[商店]

    【讨论】:

      【解决方案2】:

      只需在 web.xml 文件中添加多参数传递代码

      [供应商名称]/[模块名称]/等

      web.xml

      <?xml version="1.0"?>
      <routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
      
      <route url="/V1/products/getproductprice/:sku" method="GET">
          <service class="Evamp\Webapi\Api\ProductsInterface" method="getProductPrice"/>
          <resources>
              <resource ref="anonymous"/>
          </resources>
      </route>
      
       <route url="/V1/products/getproductattribute/:sku/:attributecode" method="GET">
                 <service class="Evamp\Webapi\Api\ProductsInterface" method="getProductAttribute"/>
                  <resources>
                    <resource ref="anonymous"/>
                  </resources>
          </route>
      

      现在在

      中创建一个接口文件

      [供应商名称]/[模块名称]/Api

      ProductsInterface.php

          interface ProductsInterface
        {
          /**
         * Returns greeting message to user
         *
         * @api
       * @param string $sku
       * @return string Greeting message with users name.
       */
      public function getProductPrice($sku);
      /**
       * @param $sku
       * @param $attributecode
       * @return mixed
       */
      public function getProductAttribute($sku, $attributecode);
      }
      

      现在创建一个实现此抽象方法的文件

      [供应商名称]/[模块名称]/模态

      class Products implements ProductsInterface
      {
      /**
       * @var ProductRepositoryInterface
       */
      private $_productRepository;
      /**
       * Product constructor.
       * @param ProductRepositoryInterface $productRepository
       */
      public function __construct(ProductRepositoryInterface $productRepository)
      {
          $this->_productRepository = $productRepository;
      }
      /**
       * Returns greeting message to user
       *
       * @param string $sku
       * @return string Greeting message with users name.
       * @throws \Magento\Framework\Exception\NoSuchEntityException
       * @api
       */
      public function getProductPrice($sku)
      {
          $prodcutBySku = $this->_productRepository->get($sku);
          return $prodcutBySku->getPrice();
      }
      /**
       * @param $sku
       * @param $attributecode
       * @return \Magento\Framework\Api\AttributeInterface|null
       * @throws \Magento\Framework\Exception\NoSuchEntityException
       */
      public function getProductAttribute($sku, $attributecode)
      {
          $prodcutBySku = $this->_productRepository->get($sku);
          return $prodcutBySku->getCustomAttribute($attributecode);
      }
      }
      

      现在创建一个 di.xml 文件,在接口文件中提供方法调用

      [供应商名称]/[模块名称]/等

      di.xml

      <?xml version="1.0"?>
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      
          <preference for="[vendorname]\[modulename]\Api\ProductsInterface"
                      type="[vendorname]\[modalname]\Model\getProductPrice" />
      
      
          <preference for="[vendorname]\[modulename]\Api\ProductsInterface"
                      type="[vendorname]\[modalname]\Model\getProductAttribute" />
      
      </config>
      

      注意:接口文件的抽象方法中的参数名称必须相同,方法实现文件中的参数名称必须相同。

      在浏览器中输入此网址

      http://<hostname>/rest/V1/products/getproductprice/<SKU of PRoduct>
      
      http://<hostname>/rest/V1/products/getproductattribute/<SKU of Product>/<Product attribute code >
      

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2018-11-07
        • 2016-01-13
        • 2012-03-12
        • 1970-01-01
        • 1970-01-01
        • 2013-11-21
        • 2012-06-12
        • 1970-01-01
        • 2019-03-21
        相关资源
        最近更新 更多