除了我们在api2.xml中提到的默认参数之外,如何在magento rest api2中传递多个参数
<routes>
<route_collection>
<route>/appointmentupdate/:appointmentid</route>
<action_type>collection</action_type>
</route_collection>
</routes>
答案 0 :(得分:0)
我在另一个api2 magento电话中找到了这个
app\code\core\Mage\Catalog\etc\api2.xml:
187: <route>/products/:id/store/:store</route>
所以url调用将是:
产品/ [ID] / [存储]
答案 1 :(得分:0)
只需在web.xml文件中为多参数传递添加代码
[供应商名称] / [模块名称] / etc
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文件,该文件在接口文件中提供方法调用
[供应商名称] / [模块名称] / etc
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>
注意:参数名称在接口文件的“抽象方法”中必须相同,在“方法实现文件”中必须相同。
在浏览器中键入此URL
http://<hostname>/rest/V1/products/getproductprice/<SKU of PRoduct>
http://<hostname>/rest/V1/products/getproductattribute/<SKU of Product>/<Product attribute code >
希望对您有所帮助。