TYPO3 v9.5.11 Extbase:将 ContainerClass 生成的 ServiceObject 注入到 Repository
TYPO3 v9.5.11 Extbase: Inject ServiceObject generated by a ContainerClass into Repository
我正在尝试将服务对象注入我的存储库。我在目录 Classes/Services 下创建了不同的服务 Classes。我还创建了一个名为 ContainerService 的 class,它为每个服务 Class.
创建并实例化一个 ServiceObject
容器服务Class:
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 Class 中创建了我的 ServiceObjects。
现在我想将 ContainerService 注入我的 Repository 并使用它。
我的存储库Class:
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 Class 中,注释和 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;
}
现在可以了。
我正在尝试将服务对象注入我的存储库。我在目录 Classes/Services 下创建了不同的服务 Classes。我还创建了一个名为 ContainerService 的 class,它为每个服务 Class.
创建并实例化一个 ServiceObject容器服务Class:
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 Class 中创建了我的 ServiceObjects。 现在我想将 ContainerService 注入我的 Repository 并使用它。
我的存储库Class:
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 Class 中,注释和 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;
}
现在可以了。