Extbase - externe Libraries

Aus Wikizone
Wechseln zu: Navigation, Suche

Zum Beispiel um FPDF oder mpdf oder ähnliche Libs zu nutzen. Im Netz finden sich verschiedene Vorgehensweisen die aber nicht alle wie gewünscht funktionieren. Grundsätzlich unterscheidet man ein einbinden über Autoload, dann steht die Lib überall zur Verfügung. Oder man included lokal. Dann muß man mit den Pfaden und Namespaces aufpassen. Außerdem werden verschiedene Verzeichnisse vorgeschlagen.

Links[Bearbeiten]

http://www.typo3.net/forum/thematik/zeige/thema/110953/?show=1

http://stackoverflow.com/questions/16539685/how-to-include-external-classes-for-extensions-in-typo3


Einfaches Einbinden über include oder require[Bearbeiten]

Beispiel PHP-Script in Resources/Private/Php/yourlib/[Bearbeiten]

http://www.typo3.net/forum/thematik/zeige/thema/110953/?show=1 (Artikel von 2012 Zugriff: 2015-08)

Externe (PHP) Libraries sollen in folgendes Verzeichnis abgelegt werden:

Resources/Private/Php/yourlib/

Anschließend kann man einfach im Controller in einer Action über

include_once(path/to/your/library/file.php);
$vimeo = new phpVimeo('xxx','xxx','xxx','xxx');

Klasse über Class Verzeichnis einbinden[Bearbeiten]

Hinweis: Benötigt Namespaces in der Klasse

http://stackoverflow.com/questions/16539685/how-to-include-external-classes-for-extensions-in-typo3 (Zugriff: 2015-08)

If you place a class in the directory yourExt/Classes/Helper/NestedDirectory/MenuHelper.php you have two ways to get the class in all other extbase files.

First way with TYPO3 > 6.0: Using namespaces

<?php
namespace YourVendor\YourExtension\Helper\NestedDirectory;

class MenuHelper {
}

?>

Now you can make an instance of this class using

$menuHelper = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance("YourVendor\\YourExtension\\Helper\\NestedDirectory\\MenuHelper")
or simply by using
<syntaxhighlight lang="php">
$menuHelper = new \YourVendor\YourExtension\Helper\NestedDirectory\MenuHelper();

Second way with TYPO3 <= 4.7

<?php
class Tx_YourExtension_Helper_NestedDirectory_MenuHelper {
}
?>

Now you can make an instance of this class using

$menuHelper = t3lib_div::makeInstance("Tx_YourExtension_Helper_NestedDirectory_MenuHelper")

or simply by using

$menuHelper = new Tx_YourExtension_Helper_NestedDirectory_MenuHelper();

The name of the class directs the class-loader to the correct path of the source file. It is very important that the file has the same name the class has.

Beispiel über t3lib (< 6.0)[Bearbeiten]

require_once t3lib_extMgm::siteRelPath('your_extension_key') . 'Path/to/the/Script.php';

Test in 6.2 mit Namespace:

require_once \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('gbrma') . 'Resources/Private/Library/mpdf56/mpdf.php';
$mpdf=new mPDF('c');

Hat bei mir in TYPO3 6.2 nicht geklappt (require schon aber nicht das erzeugen eines neuen Objektes -> Fehler Class 'Geobit\Gbrma\Controller\mPDF' not found

Library über ext_autoload.php einbinden[Bearbeiten]

http://www.schmidtpublic.de/web-knowhow/article/pdf-mit-extbasefluid-erzeugen/

http://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Autoloading/Index.html

ext_autoload.php

$libraryClassesPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('your_ext_key') . 'Relative/Path/Of/Your/External/Library/';
return array(
'class_name_to_call' => $libraryClassesPath . 'class_file.php',
);

An example using "FPDF"

Download the library and put it in /typo3conf/ext/myext/Resources/Private/Library/fpdf/

Save this code in /typo3conf/ext/myext/ext_autoload.php

    $libraryClassesPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('myext') . 'Resources/Private/Library/';
    return array(
        'FPDF' => $libraryClassesPath . 'fpdf/fpdf.php',
    );
   clear the cache

use FPDF everywhere in your extension by calling:

    $pdf = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('FPDF');
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();