2

I need to add the quite excellent PHPWord - https://github.com/PHPOffice/PHPWord to a Joomla 2.5 project.

I have added the PHPWord files to the plugins folder (might not be the correct choice) and have the following code

    $phpWord = new \libraries\PhpOffice\PhpWord\PhpWord();

The class is not found as it is not autoloading. How would I load these classes in Joomla and it there a better folder than plugins that will will autoload?

I have added these lines

   JLoader::discover('PhpWord', JPATH_LIBRARIES . '/PhpOffice/PhpWord/'); 
   $phpWord = new PhpWord; 

Inside the PhpWord folder is Phpword.php and autoloader.php, as well as several other scripts.

4

1 回答 1

2

正如在Using_own_library_in_your_extensions上面的评论中所说,他们解释了如何为 Joomla 2.5 和 Joomla 3 导入库。

现在假设教程是正确的并且JLoader::discover正在运行,我会说问题是您在插件文件夹中有 PHPWord 并且使用 JLoader::discover 您正在搜索/libraries/PhpOffice/PhpWord/.

所以

  1. 在库中复制 /PhpOffice/PhpWord/
  2. 尝试再次使用JLoader::discover('PhpWord', JPATH_LIBRARIES . '/PhpOffice/PhpWord/');

看了PhpWord的文档发现也可以直接require这个库,这样使用:

// be sure that the path match in your joomla
require_once 'libraries/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();
于 2015-03-18T04:04:31.257 回答