Way to require an autoload in one file on a Presta

2019-02-20 07:18发布

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!

2条回答
smile是对你的礼貌
2楼-- · 2019-02-20 08:16

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!
  }
查看更多
够拽才男人
3楼-- · 2019-02-20 08:20

Maybe to re-route all scripts to one using htaccess/rewrite, then in that one before including prestashop files use autoload.

查看更多
登录 后发表回答