Prestashop - 我的模块的 REST 端点
Prestashop - REST endpoints for my module
我正在开发 Prestashop 模块,它将导出客户数据和订单,它将包含用于客户同步、购物车和订单事件的挂钩 - 通常是与 CRM 类服务集成的模块。
我的模块包含它自己的视图,在 vue.js 中创建 - 单页,异步。有注册、登录、设置等页面。与后端的通信是通过 {baseUrl}/mymodule/actionname 路由上的 GET/POST 请求和 vue 视图所依赖的简单 json 响应进行的。我只需要为我的模块创建 REST 端点,就像下面的示例一样。
WordPress 自定义 RestApi:
class RestApi
{
public function __construct()
{
add_action('rest_api_init', array(get_class($this),
'register_endpoints'));
}
public static function register_endpoints()
{
register_rest_route('mymodule', '/login', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array('RestApi', 'login' ),
));
}
}
SugarCRM 自定义 RestApi:
class ModuleRestApi extends SugarApi
{
public function registerApiRest()
{
return [
'moduleLogin' => [
'reqType' => 'POST',
'noLoginRequired' => true,
'path' => [
'mymodule', 'login'
],
'method' => 'login'
],
];
}
}
我在 PrestaShop 中找不到类似的解决方案,presta 文档中没有关于自定义端点的字样,我尝试将 FrontModuleControllers 与友好的 url 一起使用,但它似乎对我不起作用,它抛出很多东西作为响应,这对我来说是无用的,当我尝试重写 init() 方法时,它也需要很多东西来实际启动控制器。我需要简单的 REST 解决方案,我可以在其中放置用于从我的视图接收数据的逻辑,将其传递到我的 CRM 服务并 return json 响应我的视图。我不需要任何更多的模板或视图渲染,只需要用于通信的路由。
PrestaShop 不支持开箱即用。但是,您可以使用模块和前端控制器来完成。
这是一个基本的例子。
1.用于注册友好 URL 的模块
class RestApiModule extends Module
{
public function __construct()
{
$this->name = 'restapimodule';
$this->tab = 'front_office_features';
$this->version = '1.0';
parent::__construct();
}
public function install()
{
return parent::install() && $this->registerHook('moduleRoutes');
}
public function hookModuleRoutes()
{
return [
'module-restapimodule-login' => [
'rule' => 'restapimodule/login',
'keywords' => [],
'controller' => 'login',
'params' => [
'fc' => 'module',
'module' => 'restapimodule'
]
]
];
}
}
2。创建抽象 REST 控制器
创建一个抽象控制器,以便实际端点可以从中扩展。在您的模块 controllers
文件夹中创建它,让我们将其命名为 AbstractRestController.php
abstract class AbstractRestController extends ModuleFrontController
{
public function init()
{
parent::init();
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
$this->processGetRequest();
break;
case 'POST':
$this->processPostRequest();
break;
case 'PATCH': // you can also separate these into their own methods
case 'PUT':
$this->processPutRequest();
break;
case 'DELETE':
$this->processDeleteRequest();
break;
default:
// throw some error or whatever
}
}
abstract protected function processGetRequest();
abstract protected function processPostRequest();
abstract protected function processPutRequest();
abstract protected function processDeleteRequest();
}
3。创建一个实际的前端控制器
在模块 controllers/front
文件夹中创建前端控制器并将其命名为 login.php
。
require_once __DIR__ . '/../AbstractRestController.php';
class RestApiModuleLoginModuleFrontController extends AbstractRestController
{
protected function processGetRequest()
{
// do something then output the result
$this->ajaxDie(json_encode([
'success' => true,
'operation' => 'get'
]));
}
protected function processPostRequest()
{
// do something then output the result
$this->ajaxDie(json_encode([
'success' => true,
'operation' => 'post'
]));
}
protected function processPutRequest()
{
// do something then output the result
$this->ajaxDie(json_encode([
'success' => true,
'operation' => 'put'
]));
}
protected function processDeleteRequest()
{
// do something then output the result
$this->ajaxDie(json_encode([
'success' => true,
'operation' => 'delete'
]));
}
}
安装模块,现在您可以点击 http://example.com/restapimodule/login
,根据请求类型,它会执行您想要的任何操作,您会得到 JSON 响应。
要添加更多端点,请将另一个 module-restapimodule-endpointname
条目添加到 hookModuleRoutes
数组和一个从 AbstractRestController
.
扩展的前端控制器
如果您还想要正确的响应代码等,您将不得不使用本机 php 函数设置 headers,因为 PrestaShop afaik 没有任何实用程序可以为您完成或使用某种库。
同样适用于您可能想要设置的任何其他 headers,例如 content-type
(默认情况下为 text/html
)。
可以使用允许从模块添加资源的 Prestashop Web 服务。此解决方案可以在标准和安全方面节省一些时间。
关于 Prestashop Webservice 模块资源的文档在这个 link:
https://webkul.com/blog/creating-prestashop-module-webservice-api/
我正在开发 Prestashop 模块,它将导出客户数据和订单,它将包含用于客户同步、购物车和订单事件的挂钩 - 通常是与 CRM 类服务集成的模块。
我的模块包含它自己的视图,在 vue.js 中创建 - 单页,异步。有注册、登录、设置等页面。与后端的通信是通过 {baseUrl}/mymodule/actionname 路由上的 GET/POST 请求和 vue 视图所依赖的简单 json 响应进行的。我只需要为我的模块创建 REST 端点,就像下面的示例一样。
WordPress 自定义 RestApi:
class RestApi
{
public function __construct()
{
add_action('rest_api_init', array(get_class($this),
'register_endpoints'));
}
public static function register_endpoints()
{
register_rest_route('mymodule', '/login', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array('RestApi', 'login' ),
));
}
}
SugarCRM 自定义 RestApi:
class ModuleRestApi extends SugarApi
{
public function registerApiRest()
{
return [
'moduleLogin' => [
'reqType' => 'POST',
'noLoginRequired' => true,
'path' => [
'mymodule', 'login'
],
'method' => 'login'
],
];
}
}
我在 PrestaShop 中找不到类似的解决方案,presta 文档中没有关于自定义端点的字样,我尝试将 FrontModuleControllers 与友好的 url 一起使用,但它似乎对我不起作用,它抛出很多东西作为响应,这对我来说是无用的,当我尝试重写 init() 方法时,它也需要很多东西来实际启动控制器。我需要简单的 REST 解决方案,我可以在其中放置用于从我的视图接收数据的逻辑,将其传递到我的 CRM 服务并 return json 响应我的视图。我不需要任何更多的模板或视图渲染,只需要用于通信的路由。
PrestaShop 不支持开箱即用。但是,您可以使用模块和前端控制器来完成。
这是一个基本的例子。
1.用于注册友好 URL 的模块
class RestApiModule extends Module
{
public function __construct()
{
$this->name = 'restapimodule';
$this->tab = 'front_office_features';
$this->version = '1.0';
parent::__construct();
}
public function install()
{
return parent::install() && $this->registerHook('moduleRoutes');
}
public function hookModuleRoutes()
{
return [
'module-restapimodule-login' => [
'rule' => 'restapimodule/login',
'keywords' => [],
'controller' => 'login',
'params' => [
'fc' => 'module',
'module' => 'restapimodule'
]
]
];
}
}
2。创建抽象 REST 控制器
创建一个抽象控制器,以便实际端点可以从中扩展。在您的模块 controllers
文件夹中创建它,让我们将其命名为 AbstractRestController.php
abstract class AbstractRestController extends ModuleFrontController
{
public function init()
{
parent::init();
switch ($_SERVER['REQUEST_METHOD']) {
case 'GET':
$this->processGetRequest();
break;
case 'POST':
$this->processPostRequest();
break;
case 'PATCH': // you can also separate these into their own methods
case 'PUT':
$this->processPutRequest();
break;
case 'DELETE':
$this->processDeleteRequest();
break;
default:
// throw some error or whatever
}
}
abstract protected function processGetRequest();
abstract protected function processPostRequest();
abstract protected function processPutRequest();
abstract protected function processDeleteRequest();
}
3。创建一个实际的前端控制器
在模块 controllers/front
文件夹中创建前端控制器并将其命名为 login.php
。
require_once __DIR__ . '/../AbstractRestController.php';
class RestApiModuleLoginModuleFrontController extends AbstractRestController
{
protected function processGetRequest()
{
// do something then output the result
$this->ajaxDie(json_encode([
'success' => true,
'operation' => 'get'
]));
}
protected function processPostRequest()
{
// do something then output the result
$this->ajaxDie(json_encode([
'success' => true,
'operation' => 'post'
]));
}
protected function processPutRequest()
{
// do something then output the result
$this->ajaxDie(json_encode([
'success' => true,
'operation' => 'put'
]));
}
protected function processDeleteRequest()
{
// do something then output the result
$this->ajaxDie(json_encode([
'success' => true,
'operation' => 'delete'
]));
}
}
安装模块,现在您可以点击 http://example.com/restapimodule/login
,根据请求类型,它会执行您想要的任何操作,您会得到 JSON 响应。
要添加更多端点,请将另一个 module-restapimodule-endpointname
条目添加到 hookModuleRoutes
数组和一个从 AbstractRestController
.
如果您还想要正确的响应代码等,您将不得不使用本机 php 函数设置 headers,因为 PrestaShop afaik 没有任何实用程序可以为您完成或使用某种库。
同样适用于您可能想要设置的任何其他 headers,例如 content-type
(默认情况下为 text/html
)。
可以使用允许从模块添加资源的 Prestashop Web 服务。此解决方案可以在标准和安全方面节省一些时间。
关于 Prestashop Webservice 模块资源的文档在这个 link:
https://webkul.com/blog/creating-prestashop-module-webservice-api/