Prestashop 中的自动加载模块 class

Autoload module class in Prestashop

我创建了一个模块,该模块具有 FrontControllerCore class 的覆盖以添加额外的 Smarty 变量。

我 运行 遇到的问题是尝试自动加载在我的模块中的控制器中引用的 class。 class 没有加载,我不知道如何将它添加到自动加载器。

安装模块时,FrontController.php 文件应位于: override\classes\controller\

所以从 FrontController.php 你可以 "include" 手动那个文件,比如:

require_once(dirname(__FILE__).'/../../../modules/servicecharges/classes/ServiceCharge.php');

此类包含没有自动加载。

您也可以使用这个覆盖 Prestashop 自动加载的免费小模块。之后所有自定义模块 class 将被自动加载。

示例路径: /modules/my_module/libs/classes/MyClass.php

Extended Api

我已经能够使用 Composer 的自动加载解决类似的问题。

您可以调用挂钩 moduleRoutes.

而不是覆盖控制器(导致与其他插件或已经使用相同覆盖的 Prestashop 安装发生冲突)

这样,您可以始终在控制器之前调用您的自动加载器:

<?php

public function hookModuleRoutes() {
  require_once __DIR__.'/vendor/autoload.php'; // And the autoload here to make our Composer classes available everywhere!
}