为了账号安全,请及时绑定邮箱和手机立即绑定

TYPO3 v9.5.11 Extbase:将ContainerClass生成的Service

TYPO3 v9.5.11 Extbase:将ContainerClass生成的Service

PHP
胡子哥哥 2022-07-16 18:42:57
我正在尝试将服务对象注入我的存储库。我在 Classes/Services 目录下创建了不同的服务类。我还创建了一个名为 ContainerService 的类,它为每个服务类创建并实例化一个 ServiceObject。容器服务类:namespace VendorName\MyExt\Service;use VendorName\MyExt\Service\RestClientService;class ContainerService {    private $restClient;         private $otherService;    /**     * @return RestClientService     */    public function getRestClient() {        $objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);        if ($this->restClient === null) {            $this->restClient = $objectManager->get(RestClientService::class);                    }        return $this->restClient;    }...正如我所说,我在 ContainerService 类中创建了我的 ServiceObjects。现在我想将 ContainerService 注入我的存储库并使用它。MyRepository 类:namespace VendorName\MyExt\Domain\Repository;use VendorName\MyExt\Service\ContainerService;class MyRepository extends Repository{        /**     * @var ContainerService     */    public $containerService;    /**     * inject the ContainerService     *          * @param ContainerService $containerService      * @return void     */    public function injectContainerService(ContainerService $containerService) {        $this->containerService = $containerService;    }    // Use Objects from The ContainerService    public function findAddress($addressId) {        $url = 'Person/getAddressbyId/'         $someData = $this->containerService->getRestClient()->sendRequest($url)    return $someData;    }在 MyController 中,我从 findAddress 函数中接收 $someData 并对其进行一些处理。但是当我调用我的页面时,我收到以下错误消息:(1/2) #1278450972 TYPO3\CMS\Extbase\Reflection\Exception\UnknownClassExceptionClass ContainerService does not exist. Reflection failed.已经尝试重新加载所有缓存并转储自动加载也无济于事。没有用 composer 安装 TYPO3。我感谢任何建议或帮助!谢谢!
查看完整描述

1 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

实际发现了问题。在 MyRepository 类中,注释和 TypeHint 存在问题:


namespace VendorName\MyExt\Domain\Repository;


use VendorName\MyExt\Service\ContainerService;


class MyRepository extends Repository

{    

    /**

     *** @var \VendorName\MyExt\Service\ContainerService**

     */

    public $containerService;


    /**

     * inject the ContainerService

     *     

     * @param \VendorName\MyExt\Service\ContainerService $containerService 

     * @return void

     */

    public function injectContainerService(\VendorName\MyExt\Service\ContainerService $containerService) {

        $this->containerService = $containerService;

    }


    // Use Objects from The ContainerService


    public function findAddress($addressId) {

        $url = 'Person/getAddressbyId/' 

        $someData = $this->containerService->getRestClient()->sendRequest($url)

    return $someData;

    }

现在它起作用了。


查看完整回答
反对 回复 2022-07-16
  • 1 回答
  • 0 关注
  • 87 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信