TYPO3:如何在 Extbase 扩展中使用外部 PHP 库(无作曲家安装)

TYPO3: How to use external PHP libraries in Extbase Extension (no composer installation)

我得到了没有 Composer 的 TYPO3 v10.2 运行。我正在创建一个扩展,并希望将一些第三方 PHP 库包含到我的 Extbase 扩展中。我已经在 TYPO3 文档中读到那些应该放在 Resources/Private/PHP/ThirdPartyLibrary 中,但是我怎样才能把它们放在那里呢? composer require 是否仍然可行,或者我应该只下载第三方库资源并在那里解压缩?如何在 Controller 中使用外部库中的 namespace/Classes 或在我的 Extension 中使用 general ? 没有 AND 和 composer 的最佳方法是什么?想知道这两种方式。到目前为止谢谢!

您需要为您的图书馆加载 class。我不确定你使用的是哪个库。

将以下代码放入您的 ext_localconf.php

<?php

$composerAutoloadFile = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::extPath('yourExtKey') . 'Resources/Private/PHP/ThirdPartyLibrary/vendor/autoload.php';
require_once($composerAutoloadFile);

现在,您可以随时随地使用库 class。确保从 TYPO3 后端转储缓存和自动加载 class!

您可以只在本地环境中使用 composer。

  1. composer init
  2. composer req <neded packages>
  3. composer u
  4. vendor/ 移动到您的 your_extension/Resources/Private/PHP/ThirdPartyLibrary/
  5. 将自动加载路径调整为您刚刚移动的vendor/

您可以查看 Shariff Extension

他们将外部库放在 Resources/Private/Shariff/vendor/

https://bitbucket.org/reelworx/rx_shariff/src/master/Resources/Private/Shariff/

并自动加载

中的文件

https://bitbucket.org/reelworx/rx_shariff/src/master/Classes/Shariff.php

使用控制器中的库

如果你想在你的控制器中使用它们,这些库需要已经有命名空间。

因为你在 TYPO3 V10 中,你现在可以使用 TYPO3 中实现的新的 symfony 依赖注入:https://usetypo3.com/dependency-injection.html

your_extension/Classes/Controller/YourController.php

/**
 * @var ThirdPartyLibrary
 */
protected $thirdPartyLibrary;

/**
 * @param ThirdPartyLibrary $thirdPartyLibrary
 */
public function __construct(ThirdPartyLibrary $thirdPartyLibrary)
{
    $this->thirdPartyLibrary = $thirdPartyLibrary;
}

your_extension/Configuration/Services.yaml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    Vendor\Namespace\:
        resource: '../Resources/Private/PHP/ThirdPartyLibrary/*'