[英]PHP-DI cannot be resolved: Entry cannot be resolved: Parameter xxxxxx of __construct() has no value defined or guessable Full definition
[英]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.