A
A
Alexander2021-11-01 14:09:40
Bitrix24
Alexander, 2021-11-01 14:09:40

Why doesn't Bitrix find a class from a custom module?

Good afternoon, help me figure out why a third-party PHP library is not connected to Bitrix

. We have Bitrix24 a boxed version, I need to add an additional PhpSpreadsheet library to the project.

I found an article where the author shows how to do this https://tomgif.ru/1c/bitrix-php- library/
The meaning of the article is to create your own module and place the library classes in the lib folder.

Based on the article, I made my own module according to the instructions in which I placed the classes of the PhpSpreadsheet library, placed the module in the local/modules/ directory and then installed it, the module installed without errors
the name of the module is PhpSpreadsheet (phpspreadsheet) it is displayed in the list of modules as installed

Now I'm trying to call the library classes in the PHP console and create an object, but Bitrix gives an error,
Class 'PhpOffice\PhpSpreadsheet\Spreadsheet' not found

Maybe I connected the library incorrectly, I can't understand?

The code I enter in the console:

use Bitrix\Main\Loader;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
Loader::includeModule('phpspreadsheet'); // подключаю мой модуль

$spreadsheet = new Spreadsheet();  // создаю объект
$sheet = $spreadsheet->getActiveSheet();

install/index.php file

<?
use Bitrix\Main\ModuleManager;

class PhpSpreadsheet extends CModule
{
  public $MODULE_ID = 'phpspreadsheet';
  public $MODULE_VERSION;
  public $MODULE_VERSION_DATE;
  public $MODULE_NAME;
  public $MODULE_DESCRIPTION;

  public function __construct()
  {
    $this->MODULE_ID = 'phpspreadsheet';
    $this->MODULE_VERSION = '0.0.1';
    $this->MODULE_VERSION_DATE = '2018-10-06 18:52:00';
    $this->MODULE_NAME = 'PhpSpreadsheet';
    $this->MODULE_DESCRIPTION = 'PhpSpreadsheet';

  }

  public function doInstall()
  {
    ModuleManager::registerModule($this->MODULE_ID);
  }

  public function doUninstall()
  {
    ModuleManager::unregisterModule($this->MODULE_ID);
  }

}


include.php file
/ 
use Bitrix\Main\Loader;
use Bitrix\Main\Application;

$application = Application::getInstance();
$modulePath = getLocalPath('modules/phpspreadsheet');
$libPath = $application->getDocumentRoot() . $modulePath . '/lib';

$iterator = new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator($libPath)
);

$classes = [];
foreach ($iterator as $file) {
  if ($file->isDir())
    continue;

  $pathName = $file->getPathname();

  $basePath = str_replace($libPath, '', $pathName);
  $name = str_replace('.php', '', $basePath);
  $name = str_replace('/', '\\', $name);
  $path = 'lib' . $basePath;
  $classes[$name] = $path;
}

Loader::registerAutoLoadClasses('phpspreadsheet', $classes);


Library https://github.com/PHPOffice/PhpSpreadsheet/tree/m...
I placed the library files in the module directory module folder/lib/PhpSpreadsheet/
There may be an error in the structure of the library, I just do not correctly specify the namespace when accessing to the library class?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2021-11-03
@gromdron

Check what you have in $classes in the include.php file.
I would not do it at all as suggested by the author, but use the composer connection to the Bitrix Framework

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question