首页 文章

Symfony未使用注入服务

提问于
浏览
1

我正在写一个Custom Symfony Service,一个Mailer,来发送我的所有邮件 . 因此,我试图在自定义服务类中注入邮件程序和模板服务 . 我已经尝试过stackoverflow的其他解决方案,例如10304468,但这对我没有帮助 .

这是我的邮件服务类

namespace Acme\DemoBundle\Mail;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Swift_Message;

class Mailer {

    protected $mailer;

    protected $templating;



public function __construct(Swift_Message $mailer, EngineInterface $templating) {

            $this->mailer = $mailer;
            $this->templating = $templating;
        }



    public function NewUserMail($newuser){

        $message = new Swift_Message();
        $message->newInstance()
                ->setSubject('Demo Subject')
                ->setFrom(array('info@example.com' => 'Your Company'))
                ->setTo($newuser->getEmail())
                ->setBody($this->templating->render('AcmeDemoBundle:Mail:newuser.html.twig'), 'text/html');              
        $this->sendMail($message);
    }


    public function sendMail($message) {
        $this->mailer->send($message);
    }
}

这是我的Service.yml(通过Dependecy Injection Extension文件加载:

acme_demo_bundle.mailer:
    class:     Acme\DemoBundle\Mail\Mailer
    arguments: ["@mailer", "@templating"]

现在当我在Controller中调用Mailer服务时,我收到此错误:

可捕获的致命错误:参数1传递给Acme \ DemoBundle \ Mail \ Mailer :: __ construct()必须是Swift_Message的一个实例,没有给出,

我如何在控制器内调用邮件服务器:

/...
$message = new Mailer();
$message->NewUserMail($newuser);
/...

如果我从容器中插入邮件程序和模板服务,在生成新的邮件程序类时,我将不会收到错误 . 但是我想,Service.yml将insertig服务放入我的 class ,但显然不是 .

我也尝试过另一个symfony项目,同样的问题 . 看起来我在这里遗漏了一些东西...... :(

1 回答

  • 0

    Symfony的 DependencyInjection 实际上并没有改变php的工作方式 . 因此 service container . 它只是一系列组件,可以帮助您集中服务(实际上是一个类)实例化 . 您可以用非常简单的术语来思考它:

    class ServiceContainer{
    
      protected $definitions;
      protected $instantiated;
    
      /**
       * @param array $definitions the service definitions, e.g: the result of parsing the yml file
       */
      public function __construct($definitions){
        $this->definitions = $definitions;
      }
    
      public function get($service){
        if(isset($this->instantiated[$service])
          return $this->instantiated[$service]);
    
        $definition = $this->definitions[$service];
        $class = $definition['class'];
        $arguments = $definition['arguments'];
        // the resolved arguments
        $args = array();
        foreach($arguments as $a){
          // resolveArguments will return the corresponding object/value depending on the value of the arg
          // i.e: it will return an instantiated service for "@mailer"
          // or the parameter's value for "%some_parameter%"
          $args[] = $this->resolveArguments($a);
        }
        // now instantiate the object base on the resolved arguments
        $reflector = new ReflectionClass($class);
        $object = $reflector->newInstanceArgs($args);
    
        $this->instantiated[$service] = $object;
    
        return $object;
      }
    }
    

相关问题