【问题标题】:Can’t get my Mailer Service working in Symfony 4无法让我的邮件服务在 Symfony 4 中工作
【发布时间】:2018-10-20 18:40:01
【问题描述】:

创建服务时,我的邮件程序无法正常工作。

我一直在学习一些教程。 我一直在尝试注入我的依赖项,但无法让我的$this->container->render() 工作

我收到以下错误消息

ServiceNotFoundException: 服务“App\Services\Mailer”依赖于不存在的服务“模板”。

在我的 Mailer 服务中注入模板服务的好方法是什么?我知道这叫做依赖注入,但我不能让它正常工作。

我试图遵循这个但没有运气:RenderView in My service

我的控制器:

use App\Services\Mailer;

class ScriptController extends AbstractController
{
    private function getThatNotifSent($timeframe, Mailer $mailer)
    {
        // Some code

        foreach ( $users as $user ) {
            $mailer->sendNotification($user, $cryptos, $news, $timeframe);
            $count++;
        }

        $response = new JsonResponse(array('Mails Sent' => $count));
        return $response;
    }
}

我的服务:

<?php
// src/Service/Mailer.php

namespace App\Services;

use Symfony\Component\DependencyInjection\ContainerInterface;


class Mailer
{
    private $mailer;
    private $templating;

    public function __construct(\Swift_Mailer $mailer ,ContainerInterface $templating)
    {
        $this->mailer = $mailer;
        $this->templating = $templating;
    }
    public function sendNotification($user, $cryptos, $news, $timeframe)
    {
        $message = (new \Swift_Message('Your Daily Digest Is Here!'))
            ->setFrom('no-reply@gmail.com')
            ->setTo($user->getEmail())
            ->setBody(
                $this->templating->render(
                    'emails/notification.html.twig',
                    array(
                        'timeframe' => $timeframe,
                        'cryptos' => $cryptos,
                        'user' => $user,
                        'news' => $news,
                    )
                ),
                'text/html'
            )
            ->setCharset('utf-8');

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

我的服务.yaml

# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: 'en'
    images_directory: '%kernel.project_dir%/public/images/blog'

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
        public: false       # Allows optimizing the container by removing unused services; this also means
                            # fetching services directly from the container via $container->get() won't work.
                            # The best practice is to be explicit about your dependencies anyway.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones
    App\EventListener\LoginListener:
        tags:
            - { name: 'kernel.event_listener', event: 'security.interactive_login' }

    App\Services\Mailer:
          arguments: ["@mailer", "@templating”]

更新:

我已更新我的代码以遵循 Taylan 的回答。我的 Mailer 服务现在如下所示(未更改 sendNotification)

<?php
// src/Service/Mailer.php

namespace App\Services;

use Symfony\Bundle\TwigBundle\TwigEngine;


class Mailer
{
    private $mailer;
    private $templating;

    public function __construct(\Swift_Mailer $mailer ,TwigEngine $templating)
    {
        $this->mailer = $mailer;
        $this->templating = $templating;
    }

我仍然收到相同的错误消息。但是在网上做了研究之后,我在阅读了这个有用的链接后将我的 framework.yaml 更新为以下内容: https://github.com/whiteoctober/BreadcrumbsBundle/issues/85

framework:
    templating: { engines: [twig] }

成功了

感谢您的帮助。

【问题讨论】:

    标签: php symfony dependency-injection symfony4


    【解决方案1】:

    ContainerInterface 类型提示为您提供容器,但您将其命名为 $templating。你应该像$this-&gt;templating = $container-&gt;get('templating')这样从容器中获取模板。

    但是你真的需要容器吗?您应该能够通过像 Symfony\Bundle\TwigBundle\TwigEngine $templating 这样的类型提示直接注入模板,而不是 Symfony\Component\DependencyInjection\ContainerInterface $container

    P.S:您可以通过php bin/console debug:container命令搜索服务。

    【讨论】:

    • 感谢您的帮助 Taylan,它帮助我让它正常工作。我还必须更新我的 framework.yaml。我会接受你的回答,但如果有人正在寻找这个问题的完整解决方案,请阅读我的更新。祝你编码好运。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多