Autoload module class in Prestashop

我是研究僧i 提交于 2019-12-08 10:50:26

问题


I've created a module that has an override for the FrontControllerCore class to add additional Smarty variables.

The issue I'm running into is trying to autoload a class that is referenced in the controller that is in my module. The class isn't being loaded and I don't know how to add it to the autoloader.


回答1:


When you install the module the FrontController.php file should be located in: override\classes\controller\

so from the FrontController.php you can "include" manually that file like:

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

There's no autoload for such includes.




回答2:


Also you can use this free tiny module that override Prestashop autoload. After that all your module custom class will be autoloaded.

Exemple path: /modules/my_module/libs/classes/MyClass.php

Extended Api




回答3:


I've been able to solve a similar problem, with Composer's autoload. Way to require an autoload in one file on a Prestashop module?

Instead of overriding a controller (leading to conflicts with other plugins or installations of Prestashop already using the same overrides) you could invoke the hook moduleRoutes.

This way, you could call your autlooader always before the controllers:

<?php

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


来源:https://stackoverflow.com/questions/29110821/autoload-module-class-in-prestashop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!