需要在 Prestashop 模块上的一个文件中自动加载的方法? [英] Way to require an autoload in one file on a Prestashop module?

查看:28
本文介绍了需要在 Prestashop 模块上的一个文件中自动加载的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一组带有 Composer 的库用于 Prestashop 模块.

I'm trying to use a set of libraries with Composer for a Prestashop module.

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

My current approach is to include the vendor/autoload.php file on every file (mymodule.php, controllers/front/foo.php, controllers/admin/bar.php, etc.)

仅在 mymodule.php 之上执行 require 不是解决方案,我看不到任何钩子来完成任务.

Doing the require only on top of the mymodule.php is not a solution, I don't see any hook to do the task.

有没有比复制 & 更好的方法?将相同的代码段粘贴到每个 PHP 文件的顶部?谢谢!

Is there a better approach than copy & paste the same snippet on top of every PHP file? Thank you!

推荐答案

我已经找到了解决方案!

I've found the way to do it!

actionDispatcher 钩子适用于模型、钩子,但不适用于控制器.

The actionDispatcher hook was working for me with models, hooks, but not with controllers.

似乎有一个未记录的钩子叫做 moduleRoutes,它在任何控制器之前加载.

Seems like there is a not documented hook called moduleRoutes which loads before any controller.

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

So I've been able to autoload in all my module's classes this way:

<?php

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

//_PS_MODULE_DIR_

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

class MyCustomModule extends DevnixPrestablocksModule { // 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!
  }

这篇关于需要在 Prestashop 模块上的一个文件中自动加载的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆