繁体   English   中英

PHP-DI 无法解析:条目无法解析:__construct() 的参数 xxxxxx 没有定义或可猜测的值 完整定义

[英]PHP-DI cannot be resolved: Entry cannot be resolved: Parameter xxxxxx of __construct() has no value defined or guessable Full definition

只是把它扔在这里是因为我找不到关于这个错误的很多信息,我花了大约 2 个小时才找到它。 脸掌

在 container.php -> DBService 中定义为:

    DBServiceInterface::class => function (ContainerInterface $c) {
        return new DBService(
            $c->get('settings.database'), 
            $c->get(SessionInterface::class), 
            $c->get(ValidatorInterface::class)
    );
    },

类型:DI\\Definition\\Exception\\InvalidDefinition 消息:无法解析条目“PVS\\HomeController”:无法解析条目“PVS\\DBService\\DBService”:__construct() 的参数 $settings 没有定义或可猜测的值完整定义:对象( class = PVS\\DBService\\DBService lazy = false __construct( $settings = #UNDEFINED# $session = get(PVS\\Helpers\\Storage\\Contracts\\SessionInterface) $validator = get(PVS\\Validation\\Contracts\\ValidatorInterface) ) ) 完整定义: Object ( class = PVS\\HomeController lazy = false __construct( $container = get(Psr\\Container\\ContainerInterface) $view = get(Slim\\Views\\Twig) $router = get(Slim\\Router) $flash = get( Slim\\Flash\\Messages) $session = get(PVS\\Helpers\\Storage\\Contracts\\SessionInterface) $db = get(PVS\\DBService\\DBService) ) ) 文件:

所以我开始在我的容器或 DBService.php 本身中寻找问题。 问题实际上出在错误消息第一行的控制器中。

HomeController 的构造函数定义为:

public function __construct (ContainerInterface $container, 
    Twig $view, 
    Router $router, 
    Messages $flash, 
    SessionInterface $session, 
    DBService $db) {  <--- Problem here

我把它改成:

    public function __construct (ContainerInterface $container, 
    Twig $view, 
    Router $router, 
    Messages $flash, 
    SessionInterface $session, 
    DBServiceInterface $db) { <--- 

请注意,我现在调用的是接口而不是具体实现,它与上面发布的 DI 容器相匹配。

我有同样的例外,简而言之,我的问题是一个类继承了其父类的构造函数,而父类__constructor不是公共的而是受保护的。

也许这可以节省其他人数小时的研究时间。

暂无
暂无

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

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