TYPO3:使用构造函数将服务 类 注入 AuthServiceClass

TYPO3: Inject Service Classes into AuthServiceClass with Constructor

我正在使用 TYPO3 10.2 并尝试将我创建的一些服务 类 注入到我的身份验证服务中。

class AuthService extends \TYPO3\CMS\Core\Authentication\AuthenticationService

AuthService 中的构造函数:

    /**
     * Contains the configuration of the current extension
     * @var ConfigurationService
     */
    protected $configurationService;

    /**
     * @var RestClientService
     */
    protected $restClientService;

    /**
     * @var ConnectionPool
     */
    protected $connectionPool;

    /**
     * 
     * @param ConfigurationService $configurationService
     * @param RestClientService $restClientService
     * @param ConnectionPool $connectionPool
     */
    public function __construct(ConfigurationService $configurationService, RestClientService $restClientService, ConnectionPool $connectionPool)
    {
        $this->configurationService = $configurationService;

        $this->restClientService = $restClientService;

        $this->connectionPool = $connectionPool;
    }

我收到以下错误:

Too few arguments to function Vendor\MyExt\Service\AuthService::__construct(), 0 passed in C:\xampp\htdocs\myproject\typo3\sysext\core\Classes\Utility\GeneralUtility.php on line 3461 and exactly 3 expected

有什么建议吗?

我在我的 ControllerClass 中使用了相同的构造函数,一切正常。

到目前为止谢谢!

看起来你的 AuthenticationService 是由 GeneralUtility::makeInstance() 内部实例化的。对于您在某个时候注册的许多 classes 来说都是如此,然后 TYPO3 负责创建 class(想想用户函数、插件控制器、模块控制器、身份验证服务、挂钩等).

GeneralUtility::makeInstance() 需要从 DI 容器中取出 class DI 才能工作,但这仅适用于在容器编译期间制作 public 的 classes .

出于这个原因,您的问题的解决方案应该是在 Configuration/Services.yaml:

中将 class AuthService 声明为 public
services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

  Vendor\MyExt\:
    resource: '../Classes/*'

  Vendor\MyExt\Service\AuthService:
    public: true

您可以在关于该主题的 official docs or in my blog post 中找到解释。