Reputation: 1837
I have a couple of example classes under the DiTest namespace;
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;
}
}
}
Then I have this yaml config file;
services:
transport:
class: DiTest\Transport
mailer:
class: DiTest\Mailer
autowire: true
Finally I have this in 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!');
It tries to instantiate the Mailer class without passing in the constructor argument. Can anyone tell me where I am going wrong.
How are we supposed to debug autowiring problems?
Upvotes: 0
Views: 606
Reputation: 1
You need to call $container->compile();
before trying to get the mailer service for all of the autowired references to be resolved.
Upvotes: 0