PHP-DI 在定义文件中设置时说缺少参数
PHP-DI says missing parameter while in definition file has set
我是 PHP-DI 的新手,我真的不明白我错过了什么。
我有一个容器:
$definitonFile = __DIR__ . '/app/etc/di_config.php';
$containerBuilder = new \DI\ContainerBuilder();
$containerBuilder->useAutowiring(true);
$containerBuilder->addDefinitions($definitonFile);
$container = $containerBuilder->build();
$container->call(["Vendor\MyTestClass", "getName"], []);
在定义文件中:
return [
'Logger' => DI\autowire()->constructor('app.log'),
];
并进入 MyTestClass
<?php
namespace Vendor;
use Monolog\Logger;
class MyTestClass
{
/**
* @var Logger
*/
private $logger;
public function __construct(Logger $logger)
{
$this->logger = $logger;
}
public function getName()
{
var_dump($this->logger);
}
}
出于某种原因,我收到此错误消息:
Fatal error: Uncaught DI\Definition\Exception\InvalidDefinition: Entry "Vendor\MyTestClass" cannot be resolved: Entry "Monolog\Logger" cannot be resolved: Parameter $name of __construct() has no value defined or guessable Full definition: Object ( class = Monolog\Logger lazy = false __construct( $name = #UNDEFINED# $handlers = (default value) array ( ) $processors = (default value) array ( ) ) ) Full definition: Object ( class = Vendor\MyTestClass lazy = false __construct( $logger = get(Monolog\Logger) ) ) in /var/www/dmholding.lh/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php on line 18
我做的和 documentation 完全一样。
有人可以看看吗?
我没有看到 Logger
class 被定义。请参阅 PHP-DI 文档中的此示例。
<?php
// config.php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
return [
// ...
Psr\Log\LoggerInterface::class => DI\factory(function () {
$logger = new Logger('mylog');
$fileHandler = new StreamHandler('path/to/your.log', Logger::DEBUG);
$fileHandler->setFormatter(new LineFormatter());
$logger->pushHandler($fileHandler);
return $logger;
}),
];
return [
'Logger' => DI\autowire()->constructor('app.log'),
];
你把Logger
定义成class的名字,但是你实际注入的class好像是Monolog\Logger
.
我是 PHP-DI 的新手,我真的不明白我错过了什么。
我有一个容器:
$definitonFile = __DIR__ . '/app/etc/di_config.php';
$containerBuilder = new \DI\ContainerBuilder();
$containerBuilder->useAutowiring(true);
$containerBuilder->addDefinitions($definitonFile);
$container = $containerBuilder->build();
$container->call(["Vendor\MyTestClass", "getName"], []);
在定义文件中:
return [
'Logger' => DI\autowire()->constructor('app.log'),
];
并进入 MyTestClass
<?php
namespace Vendor;
use Monolog\Logger;
class MyTestClass
{
/**
* @var Logger
*/
private $logger;
public function __construct(Logger $logger)
{
$this->logger = $logger;
}
public function getName()
{
var_dump($this->logger);
}
}
出于某种原因,我收到此错误消息:
Fatal error: Uncaught DI\Definition\Exception\InvalidDefinition: Entry "Vendor\MyTestClass" cannot be resolved: Entry "Monolog\Logger" cannot be resolved: Parameter $name of __construct() has no value defined or guessable Full definition: Object ( class = Monolog\Logger lazy = false __construct( $name = #UNDEFINED# $handlers = (default value) array ( ) $processors = (default value) array ( ) ) ) Full definition: Object ( class = Vendor\MyTestClass lazy = false __construct( $logger = get(Monolog\Logger) ) ) in /var/www/dmholding.lh/vendor/php-di/php-di/src/Definition/Exception/InvalidDefinition.php on line 18
我做的和 documentation 完全一样。
有人可以看看吗?
我没有看到 Logger
class 被定义。请参阅 PHP-DI 文档中的此示例。
<?php
// config.php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
return [
// ...
Psr\Log\LoggerInterface::class => DI\factory(function () {
$logger = new Logger('mylog');
$fileHandler = new StreamHandler('path/to/your.log', Logger::DEBUG);
$fileHandler->setFormatter(new LineFormatter());
$logger->pushHandler($fileHandler);
return $logger;
}),
];
return [
'Logger' => DI\autowire()->constructor('app.log'),
];
你把Logger
定义成class的名字,但是你实际注入的class好像是Monolog\Logger
.