创建自定义资源Web服务PrestaShop

时间:2018-02-16 08:51:55

标签: php web-services prestashop prestashop-1.6

我为PrestaShop 1.6创建了一个模块,我在 mymodule / mymodule.php 中创建了一个表:

class Mymodule extends Module {

    // Some code

    public function installDb() {
        return Db::getInstance()->execute("
        CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "mytable`(
        `id_mdm` INT NOT NULL AUTO_INCREMENT,
        `id_category` INT NOT NULL,
        `service` INT NOT NULL,
        `title` VARCHAR(300) NOT NULL default '',
        `title_font_size` VARCHAR(128) NOT NULL default '',
        `title_color` VARCHAR(128) NOT NULL default '',
        `background_color` VARCHAR(128) NOT NULL default '',
        `border_style` VARCHAR(128) NOT NULL default '',
        `position` INT NOT NULL,
        `count` INT NOT NULL,
        PRIMARY KEY (`id_mdm`), UNIQUE (`id_category`)) ENGINE = InnoDB;");
    }

    // Some code

}

工作正常,我的桌子已经创建了。然后我在 mymodule / override / classes / webservice / WebserviceRequest.php 中覆盖webservice:

class WebserviceRequest extends WebserviceRequestCore {
    public static function getResources() {
        $resources = parent::getResources();
        $resources['myresource'] = array(
            'description' => '',
            'class' => 'myresource'
        );
        ksort($resources);
        return $resources;
    }
}

我在 mymodule / override / classes / Myresource.php 中创建了一个名为myresource的新类:

class MyresourceCore extends ObjectModel {
    public $id;
    public $id_mdm;
    public $id_category;
    public $service;
    public $title;
    public $title_font_size;
    public $title_color;
    public $background_color;
    public $border_style;
    public $position;
    public $count;

    public static $definition = array(
        'table' => 'mytable',
        'primary' => 'id_mdm',
        'fields' => array(
            'id_category' => array('type' => self::TYPE_INT),
            'service' => array('type' => self::TYPE_INT),
            'title' => array('type' => self::TYPE_STRING),
            'title_font_size' => array('type' => self::TYPE_STRING),
            'title_color' => array('type' => self::TYPE_STRING),
            'background_color' => array('type' => self::TYPE_STRING),
            'border_style' => array('type' => self::TYPE_STRING),
            'position' => array('type' => self::TYPE_INT),
            'count' => array('type' => self::TYPE_INT)
        )
    );

    protected $webserviceParameters = array();
}

在后台办公室,我为 myresource 生成了一个密钥,但是当我在浏览器 http://mydomain/api/myresource?ws_key=mykey 中进行测试时,出现以下错误:

Fatal error: Class 'myresource' not found in /path/mydomain/classes/webservice/WebserviceRequest.php on line 502

我不知道为什么PrestaShop没有检测到它。提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

如果检查PHP错误日志,您会注意到找不到Class类型的错误。在这种情况下,找不到“ MyResource”类。

为了解决这个问题,您需要在这样的重写方法的构造函数中包含Model类

class WebserviceRequest extends WebserviceRequestCore {
    public function __construct()
    {
         include_once(_PS_MODULE_DIR_ . 'myresource' . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'MyResource.php');
    }

    public static function getResources()
    {
        $resources = parent::getResources();
        $resources['myresource'] = array(
            'description' => '',
            'class' => 'myresource'
        );
        ksort($resources);
        return $resources;
    }
}

您需要将模型类放在 /mymodule/classes/MyResource.php

将模型类放在 mymodule / override / classes / Myresource.php 中是不正确的,因为没有Myresource类可以覆盖。这将在卸载模块时给您一个错误-您将无法卸载

答案 1 :(得分:0)

在Prestashop 1.7中,您可以使用以下钩子:addWebserviceResources

示例:

include_once dirname(__FILE__) . '/classes/Sample.php';

class myAPISample extends Module {

    // ...

    public function install() {
        return parent::install() && $this->registerHook('addWebserviceResources');
    }

    // ...

    public function hookAddWebserviceResources($params) {
         return [ 'samples' => ['description' => 'My sample', 'class' => 'Sample' ] ];
    }

    //...
}

另请参见(法语):https://www.h-hennes.fr/blog/2018/06/25/prestashop-ajouter-un-objet-dans-lapi/