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

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

问题描述



我目前的方法是包括供应商/ autoload.php 文件( mymodule.php controllers / front / foo.php controllers / admin / bar.php 等)



mymodule.php 不是一个解决方案,我没有看到任何钩子来完成这项任务。



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

解决方案

我找到了办法!



actionDispatcher 钩子对于模型,钩子,但不是控制器。



似乎有一个没有记录的钩子<



因此,我已经可以通过这种方式在所有模块的类中自动加载:

 <?php 

if(!defined('_ PS_VERSION_'))
出口;

// _ PS_MODULE_DIR_

require_once __DIR __。'/ vendor / autoload.php'; //自定义模块定义

类MyCustomModule扩展Devnix \Prestablocks\Module {//我的自定义Prestashop框架(在实验阶段,https://github.com/devnix/prestablocks)

// ...

public function install(){
return
parent :: install()&&
$ this-> registerHook('moduleRoutes'); //注册钩子
}


public function hookModuleRoutes(){
require_once __DIR __。'/ vendor / autoload.php'; //这里的自动加载使我们的Composer类随处可用!
}


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

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.)

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

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!

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

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 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!
  }

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

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