在TYPO3 V 6.1中使用外部库作为扩展

时间:2013-07-22 09:25:47

标签: namespaces typo3 extbase

根据TYPO3 V6中的http://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Autoloading/Index.html,鼓励使用名称空间,并且任何PHP文件都只应包含一个类。 引自上面的链接

 - In TYPO3 every class must reside in its own file, i.e. there should
   be only one class per PHP file
 - Use the class naming convention and file location.

我的扩展程序是使用扩展构建器构建的。 它使用twitter API库,并且有一个文件config.php将被使用。 该文件中包含multiple classes

问题是,要使用这个config.php,遵循这两个条件,我应该将config.php分成多个php文件,每个文件中只有一个类吗?

或者有一个非常巧妙的方法来解决这个问题吗?

2 个答案:

答案 0 :(得分:3)

保留外部代码。编码指南仅适用于扩展和核心开发本身,您无需修改​​外部库以符合该指南。

只需将外部脚本包含在

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

并开始使用它们。

答案 1 :(得分:3)

通过以下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',
);

使用" FPDF"

的示例
  1. 下载图书馆并将其放入 / typo3conf / EXT / myext /资源/专用/库/ FPDF /
  2. 将此代码保存在/typo3conf/ext/myext/ext_autoload.php

    $libraryClassesPath = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('myext') . 'Resources/Private/Library/';
    return array(
        'FPDF' => $libraryClassesPath . 'fpdf/fpdf.php',
    );
    
  3. 清除缓存

  4. 通过调用:

    在您的扩展程序中的任何位置使用FPDF
    $pdf = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('FPDF');
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();