无法让我的邮件服务在Symfony 4中运行
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法让我的邮件服务在Symfony 4中运行相关的知识,希望对你有一定的参考价值。
创建服务时,我无法让Mailer工作。
我一直在关注一些教程。我一直在尝试注入我的依赖项,但没有办法让我的$this->container->render()
工作
我收到以下错误消息
ServiceNotFoundException:服务“App Services Mailer”依赖于不存在的服务“模板”。
在我的邮件服务中注入模板服务的好方法是什么?我知道这称为依赖注入,但我无法正常工作。
我试图遵循这个但没有运气:RenderView in My service
我的控制器:
use AppServicesMailer;
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 AppServices;
use SymfonyComponentDependencyInjectionContainerInterface;
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
AppController:
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
AppEventListenerLoginListener:
tags:
- { name: 'kernel.event_listener', event: 'security.interactive_login' }
AppServicesMailer:
arguments: ["@mailer", "@templating”]
更新:
我更新了我的代码,以跟随Taylan回答。我的邮件服务现在看起来如下(sendNotification Made没有变化)
<?php
// src/Service/Mailer.php
namespace AppServices;
use SymfonyBundleTwigBundleTwigEngine;
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] }
有效
谢谢你的帮助。
ContainerInterface typehint为您提供容器,但您将其命名为$templating
。你应该从像$this->templating = $container->get('templating')
这样的容器中得到模板。
但是你真的需要容器吗?您应该能够通过类型提示直接注入模板,如qazxsw poi而不是qazxsw poi。
P.S:您可以通过SymfonyBundleTwigBundleTwigEngine $templating
命令搜索服务。
以上是关于无法让我的邮件服务在Symfony 4中运行的主要内容,如果未能解决你的问题,请参考以下文章
无法自动装配服务 FOSUserBundle,Symfony 3.4
在 SF3.4 及更高版本中运行 Symfony DIC 烟雾测试