繁体   English   中英

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;
    }

我收到以下错误:

函数 Vendor\\MyExt\\Service\\AuthService::__construct() 的参数太少,0 传入 C:\\xampp\\htdocs\\myproject\\typo3\\sysext\\core\\Classes\\Utility\\GeneralUtility.php 在第 3461 行,正好是 3 个预期

任何建议这里发生了什么?

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

到目前为止,谢谢!

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

GeneralUtility::makeInstance()需要将类从 DI 容器中取出才能使 DI 工作,但这仅适用于在容器编译期间public类。

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

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

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

  Vendor\MyExt\Service\AuthService:
    public: true

您可以在官方文档或我关于该主题的博客文章中找到对此的解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM