duanhui3759 2019-05-02 09:19
浏览 64

如何在没有命名空间的情况下将PHP库添加到TYPO3扩展名?

I'm tyring to implement a custom TYPO3 extension to execute some php code. With my main class "hello world!" ist already working and i understand the use of namespaces. But now I found a php Library that suits my needs. I pasted the lib folder in the "Classes" folder of my extension. But now I'm getting class not found errors because none of the lib classes have a namespace.

Unfortunately I couldn't find any tutorial/doc on how to add a library to a typo3 extension while dynamically adding a namespace to every class. I tried to override every class with a namespace but somehow this cant be the solution

here is a sample of my Main class that is working but as soon as I try to call "ServiceMailman" i get namespace error, well, because they have none

namespace Htwg\GiMailman;

require_once 'Service/ServiceMailman.php';

class GiMailman{
    public function getMailinglists() {
        $mm = new ServiceMailman('http://localhost', '', '');
    }
}   

I'm looking for a way to add a php library to the "Classes" folder without adding a namespace to every library class.

Update: I installed the library on an externel path and added it to the composer.json in the classmap entry:

"autoload": {
    "psr-4": {
        "Htwg\\GiMailman\\": "Classes/"
    },
    "classmap": ["/opt/lampp/lib/php/Services"]
}

and it shows up in the autoload_classmap.php:

// autoload_classmap.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    ...
    'Services_Mailman' => $baseDir . '/../../../../lib/php/Services/Mailman.php',
    'Services_Mailman_Exception' => $baseDir . '/../../../../lib/php/Services/Mailman/Exception.php',
);

But when i try to class the class in my Main php class it still can't be found:

namespace Htwg\GiMailman;

//require_once 'Services/Mailman.php';

class GiMailman{
    public function getMailinglists() {

        $mm = new \Service_Mailman('http://localhost:8001/3.1', '', 'password');

        return "getMailinglists";

    }
}
  • 写回答

2条回答 默认 最新

  • dph19153 2019-05-02 09:23
    关注

    Any PHP classes that do not use namespaces are in the top level namespace. So you can use them like:

    $mm = new \ServiceMailman('http://localhost', '', '');
    

    You should not add external libraries to you Classes directory. Classes in this directory are autoloaded with the correct namespace for your extension (Vendor/ExtensionName). As external libraries have a different, or in your case no, namespace, this will cause problems. Usually we put external libraries in Resources/Private/Php/LibraryName. You will then need to require or include the library.

    If you're using composer it is however better not to include external libraries inside your extension, but let composer worry about it if possible. That way you don't have to worry about autoloading (so you don't need to require or include any files manually) and any dependencies for the external library are also automatically resolved. Either require the library in your global composer.json or, if you install the extension that requires it through composer, add it to the composer.json of the extension.

    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序