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

2

6

I got TYPO3 v10.2 running without Composer. I am creating an extension and want to include some third party PHP libraries into my Extbase Extension. I already read in the TYPO3 docs that those should be placed in Resources/Private/PHP/ThirdPartyLibrary but how can I include them there ? Is it still possible with composer require or should I just download the third party library resources and unzip it there ? How can I use the namespace/Classes from the external Library in the Controller or general in my Extension ? What is the best way to do this without AND with composer ? Would like to know both ways. Thanks so far!

Potful answered 22/1, 2020 at 9:39 Comment(0)
P
5

You can just use composer in your local environment.

  1. composer init
  2. composer req <neded packages>
  3. composer u
  4. Move the vendor/ into your your_extension/Resources/Private/PHP/ThirdPartyLibrary/
  5. Adjust the autoload path into the vendor/ that you just moved.

You can have a look into the Shariff Extension

They placed the external library in Resources/Private/Shariff/vendor/

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

And Autoload the files in

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

Using the libraries in your controller

The libraries need to have namespaces already if you want to use them in your controller.

Since your in TYPO3 V10 you can use the new symfony dependency injection that is implemented into TYPO3 now: 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/*'
Pines answered 22/1, 2020 at 9:55 Comment(0)
B
3

You will need to load the class for your library. I not sure which library your using.

Place the below code in your ext_localconf.php

<?php

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

Now, you can use the library class wherever you want to use it. Make sure you dump cache as well as autoload class from TYPO3 Backend!

Breaker answered 22/1, 2020 at 9:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.