要求在 Prestashop 模块上的一个文件中自动加载的方法?

Way to require an autoload in one file on a Prestashop module?

我正在尝试将一组库与 Composer 一起用于 Prestashop 模块。

我目前的做法是在每个文件(mymodule.phpcontrollers/front/foo.phpcontrollers/admin/bar.php 等)中包含 vendor/autoload.php 文件

只在 mymodule.php 之上执行 require 不是解决方案,我没有看到任何挂钩来完成任务。

是否有比在每个 PHP 文件顶部复制并粘贴相同片段更好的方法?谢谢!

我找到方法了!

actionDispatcher 挂钩适用于模型、挂钩,但不适用于控制器。

似乎有一个名为 moduleRoutes 的未记录的挂钩,它在 任何控制器之前加载。

所以我已经能够通过这种方式自动加载我所有模块的 类:

<?php

if (!defined('_PS_VERSION_'))
    exit;

//_PS_MODULE_DIR_

require_once __DIR__.'/vendor/autoload.php'; // Autoload here for the module definition

class MyCustomModule extends Devnix\Prestablocks\Module { // My custom Prestashop framework (in experimental phase, https://github.com/devnix/prestablocks)

  // ...

  public function install() {
    return
      parent::install() &&
      $this->registerHook('moduleRoutes'); // Register the hook
  }


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