我在DiTest命名空间下有几个示例类;
namespace DiTest;
class Transport
{
public function send($mail)
{
echo $mail . PHP_EOL;
echo 'Mail Sent';
}
}
class Mailer
{
protected $transport;
public function __construct(Transport $transport)
{
$this->transport = $transport;
}
public function send($mail)
{
if ($this->transport) {
$this->transport->send($mail);
} else {
echo 'No transport set!' . PHP_EOL;
}
}
}
然后我有这个yaml配置文件;
services:
transport:
class: DiTest\Transport
mailer:
class: DiTest\Mailer
autowire: true
最后我在index.php
中有这个use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
require_once __DIR__.'/vendor/autoload.php';
$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '\src'));
$loader->load('services.yml');
$mailer = $container->get('mailer');
$mailer->send('Hello world!');
它试图在不传递构造函数参数的情况下实例化Mailer类。谁能告诉我哪里出错了。
我们如何调试自动装配问题?
答案 0 :(得分:1)
在尝试获取所有自动连接引用的邮件服务之前,您需要致电$container->compile();
。