0

I have PHPWORD in /libraries/PhpOffice/PhpWord/ directory. I'm trying to use it in completly other part of app (Joomla website) like this:

require_once JPATH_LIBRARIES.'/vendor/autoload.php'; //composer
require_once JPATH_LIBRARIES.'/PhpOffice/PhpWord/PhpWord.php';
$Wzor = new \PhpOffice\PhpWord\TemplateProcessor(JPATH_ROOT.'/images/powiadomienia/SzablonPowiadomienia.docx');

But I get error:

Class 'PhpOffice\PhpWord\TemplateProcessor' not found

I tried many variants and paths, I tried to read online help but always with the same result. Can I ask for for some help and guidance? How can I include such class to my script?

Dave
  • 5,108
  • 16
  • 30
  • 40
Barto
  • 469
  • 1
  • 6
  • 15
  • Why wouldn't you just load it as part of Composer? https://github.com/PHPOffice/PHPWord#installation – miken32 Apr 09 '19 at 15:28

3 Answers3

2

Use the /vendor folder, not "libraries" (see why below).

In your project root folder:

composer require phpoffice/phpword

Then:

require_once JPATH_ROOT.'/vendor/autoload.php';
$Wzor = new \PhpOffice\PhpWord\TemplateProcessor(JPATH_ROOT.'/images/powiadomienia/SzablonPowiadomienia.docx');

WARNING:

NEVER run a "composer require" inside the "/libraries" folder as it WILL DELETE the Joomla core on more recent versions. This happens because people were using this folder and to emphatize that it shouldn't be used, the Joomla core team removed the composer.json file from it. Because of this, the Joomla core is wiped out when trying to add libraries on this folder.


Why not use "/libraries"?

From https://docs.joomla.org/J3.x:Using_Composer_with_Joomla:

Can you install any extra libraries via Composer?

Not at the moment in Joomla! 3.4. In the longer term it is the CMS's aim that you can download extra packages. To emphasize this, we will not yet be releasing the composer.json file within the main Joomla! download.

Daniel Loureiro
  • 4,595
  • 34
  • 48
  • Thank You. But I wasn't using composer directly. PHPWord was allready in library directory. I was just looking the way to include class into script. From what I found JLoader::registerNamespace was just needed to find this classes. – Barto Apr 11 '19 at 06:42
1

From what I found out problem was used framework - Joomla. It has its own method to discover classes. I had to put files into /libraries/src folder and then put this code to use this class:

JLoader::registerNamespace('PhpOffice', JPATH_LIBRARIES . '/src');
$objPHPWord = new \PhpOffice\PhpWord\PhpWord();
Barto
  • 469
  • 1
  • 6
  • 15
0

you need to install phpoffice/phpword with composer

composer require phpoffice/phpword