我正在尝试将服务对象注入到我的存储库中。我在“类/服务”目录下创建了不同的“服务类”。我创建了一个名为ContainerService的类,该类为每个服务类创建并实例化一个ServiceObject。
ContainerService类别:
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.
已经尝试重新加载所有缓存,并且转储自动加载也无济于事。未在作曲家中安装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;
}
现在可以使用。