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
7
  • 3
    Why do you include library inside of module? I think better define it as composer dependency for your module.
    – KAndy
    May 3, 2016 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, 2016 at 9:09
  • How do you use AbstractRequest? Do you inherit from it, or request it in constructor?
    – Anton Kril
    May 4, 2016 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, 2016 at 13:25
  • @Alex Hi. If you found any solution then please answer it here. i am facing same issue. May 6, 2016 at 5:16

4 Answers 4

7

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

2
  • 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, 2018 at 14:07
  • Such a smooth method to include third party module and to use it in your custom module +1 Jan 10, 2020 at 12:02
4

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
  • 1
    If i dont want to installed library via composer and direct to use copy file into extension So whats is way ? Aug 23, 2017 at 9:27
3

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

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

You can add a your library out of "Mymodule" folder as a different library and rename it to the "MymoduleLibrary" and put a composer for that without registration.php like following structure:

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


  MymoduleLibrary
    Request
       AbstractRequest
    composer.json

You need a composer like following composer

    {
   "name": "MyName/MymoduleLibrary",
   "type": "library",
   "description": "MyName library",
   "version": "3.7.1",
   "homepage": "",
   "license": "Apache-2.0",
   "authors": [
      {
         "name": "MymoduleLibrary",
         "email": ""
      }
   ],
   "autoload": {
      "psr-4": {
          "MyName\\MymoduleLibrary\\": ""
      }
   }
}

as a conclusion you have a module as "Ekomi" with registratin.php that it is possible to install and you have a library as "MymoduleLibrary" without registration but it has composer and it is valid for Magento 2 validator. Also during the running compile as you can see in the following command it does not return any error:

php bin/magento setup:di:compile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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