PrestaShop 1.7 添加新资源和类

Mar*_*ski 5 php prestashop prestashop-1.6 prestashop-1.7

我使用以下代码创建了新资源:

class WebserviceRequest extends WebserviceRequestCore {
public static function getResources(){
    $resources = parent::getResources();

    // if you do not have class for your table
    $resources['test'] = array('description' => 'Manage My API', 'specific_management' => true);

    $resources['categoryecommerce'] = array('description' => 'o jacie marcin', 'class' => 'CategoryEcommerce');

    $mp_resource = Hook::exec('addMobikulResources', array('resources' => $resources), null, true, false);
    if (is_array($mp_resource) && count($mp_resource)) {
        foreach ($mp_resource as $new_resources) {
            if (is_array($new_resources) && count($new_resources)) {
                $resources = array_merge($resources, $new_resources);
            }
        }
    }
    ksort($resources);
    return $resources;
}
}
Run Code Online (Sandbox Code Playgroud)

和新班级:

class CategoryEcommerceCore extends ObjectModelCore {

public $category_id;
public $category_core_id;

public static $definition = array(
    'table' => "category_ecommerce",
    'primary' => 'category_id',
    'fields' => array(
        'category_core_id' => array('type' => self::TYPE_INT),
    )
);

protected $webserviceParameters = array();

}
Run Code Online (Sandbox Code Playgroud)

网络服务被正确覆盖。我的类 WebserviceRequest 正在复制到 /override/classes/webservice/WebserviceRequest 但是当我安装我的模块时类没有复制到 /override/classes/ 。

如何用自己的逻辑添加新资源?我想添加与我的表格相关的类别。

问候马丁

meg*_*tur 5

一旦除了 Webkul 教程之外几乎没有任何关于 API 的内容......我试图实现“Webkul 的”教程,但也失败了。但是,似乎最好使用hooks而不是覆盖。我使用我的“逆向工程技能”来确定创建该 API 的方式,太棒了,看!:D

假设您有一个自定义的 PrestaShop 1.7 模块。您的文件是mymodule.php,这里有几个步骤。

  1. 这是一个安装方法,它允许您在数据库中注册钩子(您可以卸载并重新安装模块以执行此方法):
公共功能安装(){

    父::安装();
    $this->registerHook('addWebserviceResources');
    返回真;
}
  1. 添加钩子监听器:
公共函数 hookAddWebserviceResources($resources) {

    $ added_resources['test'] = [
        '说明' => '测试',
        'specific_management' => 真,
    ];
    返回 $ added_resources;
}

specific_management选项表明您将使用 WebsiteSpecificManagement 文件而不是数据库模型文件。

  1. 创建 WebsiteSpecificManagement 文件,称为WebsiteSpecificManagementTest(测试 - 是端点的 CamelCased 名称)。您可以从/classes/webservice/WebserviceSpecificManagementSearch.php. 删除所有内容,除了:

    • 设置对象输出
    • 设置对象
    • 获取对象
    • 获取对象输出
    • 设置网址段
    • 获取网址段
    • getContent(应该返回$this->output;,仅此而已)
    • 管理 - 您应该重写它以返回/处理您想要的数据。
  2. 添加

include_once(_PS_MODULE_DIR_.'YOURMODULENAME/classes/WebserviceSpecificManagementTest.php');

到您的模块文件(还没有弄清楚如何自动包含)。

  1. 转到/Backoffice/index.php?controller=AdminWebservice并为您的应用程序设置新的“身份验证”密钥,test从权限列表中选择端点。记住钥匙。

  2. 访问/api/test?ws_key=YOUR_KEY_GENERATED_ON_STEP_4并查看 XML 响应。

  3. 添加&output_format=JSON到您的 URL 以查看 JSON 格式的响应。你必须使用像$this->output = json_encode(['blah' => 'world'])manage的方法WebsiteSpecificManagementTest