2

I am trying to include an external PHP library inside my Magento 2 module, specifically the eKomi PHP library (https://github.com/ekomi-dev/ekomi-api).

I have added the PHP files into the module so the folder structure looks like:

MyName
  MyModule
    Ekomi
      Request
        ..
      Service
        ..
      Api.php
    etc
      ..
    Helper
      ..
    composer.json
    registration.php

My composer.json file looks like this:

{
  "name": "MyName/MyModule",
  "description": "N/A",
  "require": {
    "php": "~5.5.22|~5.6.0|~7.0.0"
  },
  "type": "magento2-module",
  "version": "1.0.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "autoload": {
    "files": [
      "registration.php"
    ],
    "psr-4": {
      "Magento\\Catalog\\": "",
      "Ekomi\\": "Ekomi/"
    }
  }
}

When I run php bin/magento setup:di:compile I am getting PHP include errors inside my Ekomi directory, I do not think I am including the files correctly inside my composer.json file.

The error message:

Compilation was started.
Proxies code generation... 0/7 [>---------------------------]   0% 1 sec 46.0 Mi
Proxies code generation... 1/7 [====>-----------------------]  14% 1 sec 50.0 Mi
Repositories code generation... 1/7 [====>-----------------------]  14% 1 sec 500 MiB
PHP Fatal error:  Class 'Ekomi\Request\AbstractRequest' not found in /var/www/magento2/app/code/MyName/MyModule/Ekomi/Request/GetProduct.php on line 8
  • 3
    Why do you include library inside of module? I think better define it as composer dependency for your module. – KAndy May 3 '16 at 16:30
  • Yeh you are right it's better that way. I'm still getting the same class not found error message though with the package inside the vendor directory – Alex May 4 '16 at 9:09
  • How do you use AbstractRequest? Do you inherit from it, or request it in constructor? – Anton Kril May 4 '16 at 11:35
  • I haven't tried to use anything yet, that error occurs during bin/magento setup:di:compile just because the files exist inside the module. It's something to do with the ekomi namespace not being allowed to be used I think, namespace must start with MyName/MyModule – Alex May 4 '16 at 13:25
  • @Alex Hi. If you found any solution then please answer it here. i am facing same issue. – Makwana Ketan May 6 '16 at 5:16
4

After searching for a long time, not finding anything readily working, I came up with a solution that works, at least it seems so. Add these lines to your registration.php:

$vendorDir = require BP . '/app/etc/vendor_path.php';
$vendorAutoload = BP . "/{$vendorDir}/autoload.php";
/** @var \Composer\Autoload\ClassLoader $composerAutoloader */
$composerAutoloader = include $vendorAutoload;
$composerAutoloader->addPsr4('Ekomi\\', array(__DIR__ . '/Ekomi'));

This idea was based on this answer to a similar question as it made me think that registration.php will always be loaded quite early in the bootstrap process, via the cli (bin/magento setup:di:compile did not fail anymore with an exception as stated above) as well as via the webserver (Yes! My module is working, ehhm no longer failing on this point :) ...).

This code actually executes what that psr-4 line ("Ekomi\": "Ekomi/") in your composer.json specifies, as that indeed does not work (for now, or with not via Magento installed packages).

  • Thanks, I will give it a go when I get a chance and accept your answer – Alex May 12 '16 at 8:42
2

You can use classes directly

If you install any library via composer it will download it in vendor folder and then you can use the class directly

$objPHPExcel = new \PHPExcel();
$objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A1', 'Hello')
    ->setCellValue('B1', 'world!');

$fn = $this->fileSystem
    ->getDirectoryWrite(DirectoryList::VAR_DIR)
    ->getAbsolutePath('/') .'report_'.time().'.xlsx';

$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save($fn);

Or if you want to copy it manually then you may have to rename class to PHPExcel_PHPExcel and you have to do this to every files it extends then you can use it by.

$objPHPExcel = new \PHPExcel_PHPExcel();
  • 1
    If i dont want to installed library via composer and direct to use copy file into extension So whats is way ? – Hitesh Vaghasiya Aug 23 '17 at 9:27
2

There's zero reason to complicate something simple, unless you want to use composer's features at the cost of overhead of using coding for it. My library isn't even on composer and I don't want to leap through hoops to just include the library so this is simple and 2 steps, using library foo as an example.

1) put the library folder in lib/internal/ making it lib/internal/foo

2) in a module you have, put this in registration.php

require_once(BP.'/lib/internal/foo/include.php');

That's it, it works and won't mess up setup:di:compile. You can now use that library in your module. Obviously include.php has to exist and be the file that will load all your libraries code, some it might be index.php, or autoload.php

  • sometimes understanding the Magento way is what we appreciate in an answer, but sometimes straightforwardness is a plus, as it is in your case, thank you ! – medmek Jun 26 '18 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.