繁体   English   中英

PHP DI - 无法解析__construct()的$ logger没有定义或猜测的值

[英]PHP DI - cannot be resolved Parameter $logger of __construct() has no value defined or guessable

我试图得到一个PHP-DI工作的基本例子,但我很容易陷入一个相当基本的例子。 我假设我在这里缺少一些简单的东西,但却无法将其单独列出来。

它没有识别出LoggerInterface类型提示,但这是直接从示例中获取的,所以我不明白我做错了什么。

当我从服务签名中删除LoggerInterface时,该示例工作正常。

服务类:

<?php
namespace test\ServiceLayer;
class TestService extends BaseService{
  public function __construct(\Psr\Log\LoggerInterface $logger){}
}
?>

config.php文件

<?php
use Monolog\Logger;
return [
    'TestService' => \DI\create(\test\ServiceLayer\TestService::class),
    Psr\Log\LoggerInterface::class => DI\factory(function () {
        $logger = new Logger('mylog');
        return $logger;
    }),
];
?>

用法:

<?php
$builder = new \DI\ContainerBuilder();
$builder->addDefinitions('config.php');
$container = $builder->build();
$service = $container->get('TestService');
?>

例外:

object(DI \\ Definition \\ Exception \\ InvalidDefinition)#115(7){
[“message”:protected] => string(196)“Entry”TestService“无法解析:__construct()的参数$ logger没有定义或可猜测的值完全定义:Object(class = arkon \\ ServiceLayer \\ TestService lazy = false) )”

您正在使用create() ,如果您希望自动装入该条目,则需要使用autowire()

请参阅此文档

暂无
暂无

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

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