Aswathy S
Aswathy S

Reputation: 729

Call to private PHPExcel_IOFactory::__construct() from context - TYPO3

Hi have to create an excel export using phpexcel library. So I copy the library in my class then write the following code in my controller

require_once PATH_site . 'typo3conf/ext/extension_name/Classes/Library/PHPExcel/IOFactory.php';

public function excelTest()
    {
        $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
        $objReader = $objectManager->get('PHPExcel_IOFactory');

    }

But it return an error

Fatal error: Call to private PHPExcel_IOFactory::__construct() from context 'TYPO3\CMS\Core\Utility\GeneralUtility' in /opt/lampp_repository/lampp-5.6/htdocs/typo3_src-7.6.6/typo3/sysext/core/Classes/Utility/GeneralUtility.php on line 4533

Upvotes: 1

Views: 1736

Answers (1)

j4k3
j4k3

Reputation: 1201

Don't include the library yourself, TYPO3 has dependency injection for that. All the php files in your Extension directory will be indexed, and all Classes inside will automatically be available, you just have to make sure, that your class cache is fresh, if in doubt by manually deleting the typo3temp/Cache/Code/ClassLoader* file.

If you want to include en external class into your own namespace, you have to hint Extbase as to how to include it, with an ext_autoload.php file, because if multiple extensions load code into the same class namespace they will collide.

It also is good practice not to inject the class itself, but an Abstrct that extends upon it, so you can customize it in an isolated manner that doesn't modify the vendor files.

Heres my approach to it:

Put all files of PHPExcel into yourextension/Classes/Vendor/PHPExcel.

Create a new file yourextension/Classes/Vendor/PHPExcel.php:

<?php
namespace Vendorname\Extensionname\Classes;

/*
 * PhpExcel
*/
class PhpExcel implements \TYPO3\CMS\Core\SingletonInterface extends \PHPExcel {

    // Differences from the  original implementation, e.g. a writer that generates 
    // a filename and puts the Excel file into typo3temp

}

You should then be able to just inject the class @inject-Annotation in your ActionController:

    /**
     * PhpExcel
     *
     * @var \Vendorname\Extensionname\Classes\PhpExcel
     * @inject
     */
    protected $phpExcel;

Further reading:

Upvotes: 1

Related Questions