CodeIgniter PHPWord using as third_party

馋奶兔 提交于 2019-12-21 20:06:08

问题


I am trying to use PHPWord and having a difficulties to use it.

This my code within application/libraries/words.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH."/third_party/PhpWord/Autoloader.php"; 

\PhpOffice\PhpWord\Autoloader::register(); \\NOT SURE IF THIS IS CORRECT

class Word extends PhpWord { 
    public function __construct() { 
        parent::__construct(); 
    } 
}

Based on PHPWord github, i must use it like this:

Alternatively, you can download the latest release from the releases page. In this case, you will have to register the autoloader.

require_once 'path/to/PhpWord/src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register(); // WHERE SHOULD I PUT THIS?

When i try to load the library from my controller as such:

$this->load->library('word');

it gives me error that says:

Fatal error: Class 'PhpWord' not found in C:\xampp\htdocs\path\to\application\libraries\Word.php on line 7

i tried to extends the Autoloader class, but PHP still complains that it can't found Autoloader.php.

when i do

file_exist(APPPATH."/third_party/PhpWord/Autoloader.php") // Similarly with PhpWord.php

it returns 1.

Does anyone know how to fix this?


回答1:


From Russia with love;) Use like this in library for CodeIgniter:

<?php 
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');


    require_once  APPPATH.'/libraries/PHPWord/src/PhpWord/Autoloader.php';
    use PhpOffice\PhpWord\Autoloader as Autoloader;
    Autoloader::register();

    class Word extends Autoloader {

    }

    ?>

In controller like code from samples:

$this->load->library('word');
/* remove ;)
#require_once 'PHPWord/src/PhpWord/Autoloader.php';
#PhpOffice\PhpWord\Autoloader::register();
*/

// Template processor instance creation
echo date('H:i:s') , ' Creating new TemplateProcessor instance...' ;
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('text.docx');


$templateProcessor->setValue('sss','123');

echo date('H:i:s'), ' Saving the result document...';
$templateProcessor->saveAs('test1.docx');

Tested it works!



来源:https://stackoverflow.com/questions/26030924/codeigniter-phpword-using-as-third-party

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!