简体   繁体   English

使用父服务的Symfony2依赖注入不起作用

[英]Symfony2 Dependency Injection with parent services does not work

I'm currently trying configure my Symfony Dependency Injection so that I'm able to use parent services. 我正在尝试配置我的Symfony依赖注入,以便我能够使用父服务。 Following the description I found in http://symfony.com/doc/current/components/dependency_injection/parentservices.html I tried to set up the following classes as a first test: 按照我在http://symfony.com/doc/current/components/dependency_injection/parentservices.html中找到的描述,我尝试将以下类设置为第一个测试:

Mailer.php: Mailer.php:

namespace testing;

class Mailer
{
    private $transport;

    public function __construct($transport) {
        $this->transport = $transport;
    }

    public function getTransport() {
        return $this->transport;
    }
}

MailManager.php: MailManager.php:

namespace testing;

abstract class MailManager
{
    protected $mailer;

    public function setMailer(Mailer $mailer)
    {
         $this->mailer = $mailer;
    }
}

NewsletterManager.php: NewsletterManager.php:

namespace testing;

class NewsletterManager extends MailManager {
    function getTransport() {
        return $this->mailer->getTransport();
    }
}

services.yml: services.yml:

 parameters:
    mailer.transport: sendmail

services:
    mailer:
        class:     testing\Mailer
        arguments: [%mailer.transport%]
    mail_manager:
        class:     testing\MailManager
        abstract:  true
        calls:
             - [ setMailer, [ @mailer ] ]
    newsletter_manager:
        class:     testing\NewsletterManager
        parent:    mail_manager

But when I try to run this configuration with the following code: 但是当我尝试使用以下代码运行此配置时:

require_once 'ClassLoading.php';
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;

$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/config'));
$loader->load('services.yml');

$nm = $container->get('newsletter_manager');
echo $nm->getTransport();

I always get this error: 我总是得到这个错误:

PHP Fatal error:  Call to a member function getTransport() on a non-object in C:\IGMI\workspace\TryingHard\loading\NewsletterManager.php on line 7

The one thing seems to be that the setter of the abstract class is never called, but even if I make the abstract class concrete and retrieve it from the container with the setter actually being called, this doesn't solve the problem of the mailer object being not set. 有一点似乎是抽象类的setter永远不会被调用,但即使我使抽象类具体化并从实际调用setter的容器中检索它,这也无法解决邮件程序对象的问题没有设定。 So it appeards to me that there is also a problem with the establishing of the subclass relationship. 所以它告诉我,建立子类关系也存在问题。

Any help would be appreciated! 任何帮助,将不胜感激!

You should compile container builder before try access services I think... so it should looks like: 您应该在尝试访问服务之前编译容器构建器我认为...所以它应该如下所示:

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;

use Symfony\Component\DependencyInjection\ContainerBuilder;

$container = new ContainerBuilder();
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/config'));
$loader->load('services.yml');

$container->compile(); //add this line

$nm = $container->get('newsletter_manager');
echo $nm->getTransport();

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

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