【问题标题】:Using external libraries in TYPO3 V 6.1 for an Extension在 TYPO3 V 6.1 中使用外部库进行扩展
【发布时间】:2013-07-22 09:25:47
【问题描述】:

根据来自 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 文件,每个文件都有一个类吗?

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

【问题讨论】:

    标签: namespaces typo3 extbase


    【解决方案1】:

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

    只需包含外部脚本

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

    并开始使用它们。

    【讨论】:

      【解决方案2】:

      关注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/Resources/Private/Library/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();
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-23
        相关资源
        最近更新 更多