Повторное получение данных конфигурации через API


Предоставляет ли Magento какой-либо API REST по умолчанию для получения данных конфигурации (system.xml)?.

Если да, то поделитесь URL-адресом.
Другие разделяют наилучший подход к получению данных конфигурации с помощью REST API.

Author: Himanshu, 2019-05-22

1 answers

В Magento есть один API Rest, который предоставляет некоторые system.xml значение поля

<route url="/V1/store/storeConfigs" method="GET">

Это обеспечивает только следующие значения полей конфигурации системы:

[
    {
        "id": 1,
        "code": "default",
        "website_id": 1,
        "locale": "en_US",
        "base_currency_code": "USD",
        "default_display_currency_code": "USD",
        "timezone": "America/Chicago",
        "weight_unit": "lbs",
        "base_url": "http://127.0.0.1/magento23dev/",
        "base_link_url": "http://127.0.0.1/magento23dev/",
        "base_static_url": "http://127.0.0.1/magento23dev/pub/static/version1554979401/",
        "base_media_url": "http://127.0.0.1/magento23dev/pub/media/",
        "secure_base_url": "http://127.0.0.1/magento23dev/",
        "secure_base_link_url": "http://127.0.0.1/magento23dev/",
        "secure_base_static_url": "http://127.0.0.1/magento23dev/pub/static/version1554979401/",
        "secure_base_media_url": "http://127.0.0.1/magento23dev/pub/media/"
    }
]

Если вы хотите указать значение конфигурации системы для всех полей или конкретное значение системного поля, вам необходимо самостоятельно создать API Rest.

Точка API, подобная:

Webapi.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 method="POST" url="/V1/devamitbera-systemconfig/systemconfig/"> <service class="Devamitbera\Systemconfig\Api\SystemconfigManagementInterface" method="getSystemconfig"/> <resources> <resource ref="anonymous"/> </resources> </route> </routes>

Интерфейс Класс: Devamitbera\Systemconfig\Api\SystemconfigManagementInterface

<?php

namespace Devamitbera\Systemconfig\Api;

interface SystemconfigManagementInterface
{

    /**
     * GET for getSystemconfig api
     * @param string $storeCode
     * @param string $path
     * @return string[]
     */
    public function getSystemconfig($storeCode =null ,$path = null);
}

Класс модели для интерфейса:

Devamitbera\Systemconfig\Model\SystemconfigManagement

Код:

<?php

namespace Devamitbera\Systemconfig\Model;

class SystemconfigManagement implements \Devamitbera\Systemconfig\Api\SystemconfigManagementInterface
{

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    private $scopeConfig;


    public function __construct(
     \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
     \Magento\Store\Model\StoreManagerInterface $storeManager       
     )
     {
         $this->scopeConfig = $scopeConfig;
         $this->storeManager = $storeManager;
    }
    /**
     * {@inheritdoc}
     */
    public function getSystemconfig($storeCode = null ,$path = null)
    {
       if($storeCode === null){
           $storeCode = $this->storeManager->getStore()->getCode();
       }
       $storeConfigFinaData = [];
       $storeConfigFinaData[][$path] = $this->scopeConfig
               ->getValue(
                       $path,
                       \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
                       $storeCode
                       );
       return $storeConfigFinaData;
    }

}

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="Devamitbera\Systemconfig\Api\SystemconfigManagementInterface" type="Devamitbera\Systemconfig\Model\SystemconfigManagement"/>
</config>

ТЕСТОВЫЙ ОБРАЗЕЦ:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://127.0.0.1/magento23dev/rest/V1/devamitbera-systemconfig/systemconfig/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\"storeCode\": \"default\",\"path\":\"contact/email/recipient_email\"}",
  CURLOPT_HTTPHEADER => array(
    "Authorization: Bearer gv5dgldyscnzpa8sveg1xfct9frnwy0q",
    "Content-Type: application/json",
    "Postman-Token: ff714339-e830-4347-8266-e458c58102d9",
    "cache-control: no-cache"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Этот метод поддерживает два параметра:

1.$storeCode : Store code
2.$path : Xpath of system.xml
 3
Author: Amit Bera, 2019-05-22 12:14:31