gpt4 book ai didi

php - 创建自定义资源 web 服务 PrestaShop

转载 作者:搜寻专家 更新时间:2023-10-31 21:20:58 24 4
gpt4 key购买 nike

我为 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 生成了一个 key ,但是当我在浏览器中测试时 http://mydomain/api/myresource?ws_key=mykey ,出现如下错误:

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

我不知道为什么 PrestaShop 没有检测到它。预先感谢您的协助。

最佳答案

在 Prestashop 1.7 中,您可以使用这个钩子(Hook):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/

关于php - 创建自定义资源 web 服务 PrestaShop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48823084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com