我的Mailer服务无法在Symfony 4中运行

时间:2018-10-20 18:40:01

标签: php symfony dependency-injection symfony4

创建服务时,我的Mailer无法正常工作。

我一直在关注一些教程。 我一直在尝试注入依赖关系,但无法让我的$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;
    }
}

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

我仍然有相同的错误消息。但是,在进行了在线研究之后,在阅读了此有用链接之后,我已经将我的framework.yaml更新为以下内容: https://github.com/whiteoctober/BreadcrumbsBundle/issues/85

framework:
    templating: { engines: [twig] }

有效

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

ContainerInterface类型提示为您提供了容器,但您将其命名为$templating。您应该从像这样的$this->templating = $container->get('templating')容器中进行模板制作。

但是您真的首先需要容器吗?您应该可以通过像这样的Symfony\Bundle\TwigBundle\TwigEngine $templating而不是Symfony\Component\DependencyInjection\ContainerInterface $container键入提示来直接插入模板。

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