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

我正在尝试将服务对象注入我的存储库。我在 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\UnknownClassException


Class ContainerService does not exist. Reflection failed.

已经尝试重新加载所有缓存并转储自动加载也无济于事。没有用 composer 安装 TYPO3。我感谢任何建议或帮助!谢谢!


胡子哥哥
浏览 88回答 1
1回答

慕神8447489

实际发现了问题。在 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;    }现在它起作用了。
打开App,查看更多内容
随时随地看视频慕课网APP