嗨,我在创造一项服务。这是密码,
namespace App\Service;
use Symfony\Component\DependencyInjection\ContainerInterface;
use App\Entity\CarAd;
class MatchCarAdService {
protected $mailer;
protected $templating;
public function __construct(ContainerInterface $container, \Swift_Mailer $mailer, $templating) {
$this->container = $container;
$this->mailer = $mailer;
$this->templating = $templating;
}
public function sendMail() {
$message = (new \Swift_Message('Hello Email'))
->setFrom('vimuths@yahoo.com')
->setTo('vimuths@yahoo.com')
->setBody(
$this->templating->render(
// templates/emails/matching-cars.html.html.twig
'emails/matching-cars.html.html.twig', []
), 'text/html'
);
$this->mailer->send($message);我是services.yml
MatchCarAdService:
class: App\Service\MatchCarAdService
arguments: ['@mailer','@templating']但我发现了这个错误,
无法解析“App\Controller\Api\SearchController()”的参数$matchService :无法自动更新服务"App\ service \MatchCarAdService":方法"__construct()“的参数"$templating”没有类型提示,您应该显式配置它的值。
发布于 2019-02-15 02:56:21
现在,构造函数有3个参数,但在参数中只放2。
因此,有两种可能的解决方案:
在yaml中的配置
MatchCarAdService:
class: App\Service\MatchCarAdService
arguments: ['@container', '@mailer','@templating']使用自动配线并输入提示--这取决于您的Symfony版本,但是将构造函数更改为
public function __construct(ContainerInterface $container, \Swift_Mailer $mailer, Symfony\Component\Templating\EngineInterface; $teplating) {
$this->container = $container;
$this->mailer = $mailer;
$this->templating = $templating;
}为了获得composer require symfony/templating服务,您可能必须使用Symfony\Bundle\FrameworkBundle\Templating\EngineInterface。
另外,必须在framework下添加以下配置
templating:
enabled: true
engines: ['twig']发布于 2019-02-17 11:09:57
Symfony 3.3+答案
回答@M. Kebza解决了你的情况。但你可以让这件事变得更简单和防虫。只需使用Symfony 3.3+特性即可。
A.使用自动装配
services:
_defaults:
autowire: true
App\Service\MatchCarAdService: ~
App\Service\CleaningService: ~
App\Service\RentingService: ~使用自动装配+自动发现-更好!
services:
_defaults:
autowire: true
App\:
resource: ../src这将通过PSR-4约定从App\目录加载../src命名空间中的所有服务。
您可以在如何重构到Symfony 3.3中的新依赖注入特性 post中看到更多的示例。
https://stackoverflow.com/questions/54701630
复制相似问题