您好我有小问题与Symfony2的依赖注入的服务,我找不出什么wrong.I遵循了http://symfony.com/doc/current/service_container/injection_types.html例子。也许有人重新审视可以排序了这一点和代码(顺便说一句,我不想延长控制器或通过这作为参数传递给类的构造函数):Symfony的2.8 dependecy注入问题
services:
app.custom_mailer:
class: NTPBundle\Mailer\CustomMailer
arguments: ['@mailer']
CustomMailer.php
namespace NTPBundle\Mailer;
class CustomMailer {
private $mailer;
public function __construct(MailerInterface $mailer)
{
$this->mailer = $mailer;
}
public function weekExtractMail() {
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('[email protected]')
->setTo('[email protected]')
->setBody("Test"
);
$this->mailer->send($message);
return $this;
}
}
和PDFTestController.php
namespace NTPBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use \Symfony\Component\HttpFoundation\Response;
class PDFTestController extends Controller{
/**
* @Route("/pdftest")
*/
public function pdfTestAction() {
$mailer=new \NTPBundle\Mailer\CustomMailer;
$mailer->weekExtractMail();
return new response($this->renderView('NTPBundle:PDFReports:weekVolumes.html.twig', array()));
}
}
和错误
可捕获的致命错误:传递给NTPBundle \ Mailer \ CustomMailer :: __ construct()的参数1必须是NTPBundle \ Mailer \ MailerInterface的一个实例,在C:\ wamp64 \ www \ src \ NTPBundle \控制器\ PDFTestController.php第19行和定义
我都对着调试容器和定义的服务。
*我在调试容器已经看过,并定义的服务。* - 那么用它代替实例自己。 – ccKep