无法让我的邮件服务在 Symfony 4 中工作

Can’t get my Mailer Service working in Symfony 4

我在创建服务时无法让我的 Mailer 工作。

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

我收到以下错误消息

ServiceNotFoundException: The service "App\Services\Mailer" has a dependency on a non-existent service "templating".

在我的 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;
    }
}

我的service.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 Made)

<?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;
    }

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

framework:
    templating: { engines: [twig] }

有效

感谢您的帮助。

ContainerInterface typehint 为您提供容器,但您将其命名为 $templating。您应该像这样从容器中获取模板 $this->templating = $container->get('templating').

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

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