Symfony 4无法在服务上注入标量值

时间:2018-03-18 11:40:08

标签: symfony symfony4

我有ff类:

namespace App\Component\Notification\RealTimeNotification;

use App\Component\Notification\NotificationInterface;

class EmailNotification implements NotificationInterface
{   
    private $logNotification;
    private $mailer;
    private $engine;

    // This will appear on From field on Email.
    private $mailerFrom;

    public function __construct(LogNotification $logNotification, \Swift_Mailer $mailer, \Twig_Environment $twig, string $from)
    {
        $this->logNotification = $logNotification;
        $this->mailer = $mailer;
        $this->twig = $twig;

        $this->mailerFrom = $mailerFrom;
    }

    public function send(array $options): void
    {   
        // Resolve options
        $this->resolveOptions($options);

        $sendTo = $options['sendTo'];
        $subject = $options['subject'];

        $template = $options['template'];
        $data = $options['data'];
        $body = $this->createTemplate($template, $data);

        $this->sendEmail($sendTo, $subject, $body);
    }

    protected function sendEmail($sendTo, $subject, $body): void
    {
        dump($this->mailerFrom);
        $message = (new \Swift_Message())
            ->setSubject($subject)
            ->setFrom($this->mailerFrom)
            ->setTo($sendTo)
            ->setBody($body, 'text/html')
        ;

        $this->mailer->send($message);
    }

    protected function createTemplate($template, $data): string
    {
        return $this->twig->render($template, $data);
    }

    protected function resolveOptions(array $options): void
    {

    }

    protected function createLog(array $email): void
    {
        $message = 'Email has been sent to: ' . $email;

        $this->logNotification->send([
            'message' => $message,
        ]);
    }
}

我尝试使用以下内容手动连接所有参数:

# Notification
app.log_notification:
    class: App\Component\Notification\RealTimeNotification\LogNotification

app.email_notification:
    class: App\Component\Notification\RealTimeNotification\EmailNotification
    decorates: app.log_notification
    decoration_inner_name: app.log_notification.inner
    arguments:
        $logNotification: '@app.log_notification.inner'
        $mailer: '@mailer'
        $twig: '@twig'
        $from: '%mailer_from%'

但是,当我运行应用程序时,它会抛出异常:

  

无法自动连接服务   “应用程序\分量\通知\ RealTimeNotification \ EmailNotification”:   方法“__construct()”的参数“$ from”必须具有类型提示或者是   明确给出一个值

为什么会发生这种事件?

谢谢!

2 个答案:

答案 0 :(得分:2)

@Matteo回答很棒!您甚至可以放弃服务定义并委托给parameter binding ,因为Smyfony 3.4 + / 2018 +

 # config/services.yaml
 services:
     _defaults:
         bind:
             $adminEmail: 'manager@example.com'

     # same as before
     App\:
         resource: '../src/*'

你想要更多的例子和逻辑吗?在此处找到它:https://www.tomasvotruba.cz/blog/2018/01/22/how-to-get-parameter-in-symfony-controller-the-clean-way/#change-the-config

答案 1 :(得分:0)

仅当您的参数是对象时,自动装配才有效。但是如果你有一个标量参数(例如一个字符串),则无法自动连接:Symfony将抛出一个明确的异常。

您应该Manually Wiring Arguments并明确配置服务,例如:

r.Use undefined (type *mux.Router has no field or method Use)
  

由于这个原因,容器会将manager@example.com传递给   创建时__construct的$ adminEmail参数   SiteUpdateManager服务。其他论点仍然是   自动装配Autowired。

希望这个帮助