簡體   English   中英

TYPO3 v9.5.11 Extbase:將ContainerClass生成的ServiceObject注入Repository

[英]TYPO3 v9.5.11 Extbase: Inject ServiceObject generated by a ContainerClass into Repository

我正在嘗試將服務對象注入到我的存儲庫中。 我在 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 類中創建了我的 ServiceObject。 現在我想將 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\UnknownClassException

Class ContainerService does not exist. Reflection failed.

已經嘗試重新加載所有緩存並且轉儲自動加載也無濟於事。 沒有用 composer 安裝 TYPO3。 我感謝任何建議或幫助! 謝謝!

實際上發現了問題。 在 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;
    }

現在它起作用了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM