Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

typo3

extbase

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!

like image 381
typo3dev Avatar asked Jan 22 '20 09:01

typo3dev


People also ask

How to install TYPO3 via composer?

You have more ways to install TYPO3 via composer: Install regular via loading the needed packages. Use Composer helper to generate the composer command. Install the cms-base-distribution package.

How do I install TYPO3 on Ubuntu?

The recommended way of installing TYPO3 is via Composer, as described on this page. If you do not want to use Composer, follow the package installation guide. You have more ways to install TYPO3 via composer: Install regular via loading the needed packages. Use Composer helper to generate the composer command.

How do I create a TYPO3 project using CMS-base distribution?

Use Composer helper to generate the composer command. Install the cms-base-distribution package. This is helpful when you are use ddev, because it configures your database and mailserver for Mailhog. To create a new TYPO3 project using the TYPO3 Base Distribution:

How do I create a TYPO3 project in ddev environment?

To create a new TYPO3 project using the TYPO3 Base Distribution in a ddev environment: For Windows users: To install TYPO3 via the composer command, it should be started as admin or explicitly given the right to create symlinks (use for example a powershell or git bash started with admin rights).


1 Answers

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/*'
like image 179
bandanh Avatar answered Sep 21 '22 16:09

bandanh