Symfony2访问邮件服务

时间:2016-05-10 11:47:45

标签: php symfony

我正在使用Symfony2并尝试访问邮件服务,但不断收到此错误消息:

{“errors”:{“code”:500,“message”:“错误:在非对象上调用成员函数get()”}}

我的代码:

<?php
namespace TestBundle\UserBundle\Utilities;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class EmailServiceClass extends Controller
{
    public function sendEmail($subject, $to, $body)
    {
        $msg = \Swift_Message::newInstance();

        $msg->setSubject($subject);
        $msg->setTo($to);
        $msg->setBody($body);
        $msg->setContentType('text/html');
        $msg->setCharset('utf-8');
        $msg->setFrom('test@gmail.com');

        $this->get('mailer')->send($msg);
    }
}

错误来自这一行:$this->get('mailer')->send($msg);

根据我的理解,当我扩展Controller调用时,我应该能够访问此服务而无需专门创建服务。

2 个答案:

答案 0 :(得分:5)

你应该采取另一种方式。

当您的服务是POPO(Plain Old PHP Object)时,它是最好的。还应该通过构造函数传递依赖项,所以让我们重构一下你的服务:

class EmailServiceClass //no need to extend anything
{

    private $mailerService; //dependency as private property

    //we're passing dependencies via constructor
    public function __construct(\Swift_Mailer $mailerService) 
    { 
        $this->mailerService = $mailerService;
    }

    public function sendEmail($subject, $to, $body)
    {
        $msg = \Swift_Message::newInstance();

        $msg->setSubject($subject);
        $msg->setTo($to);
        $msg->setBody($body);
        $msg->setContentType('text/html');
        $msg->setCharset('utf-8');
        $msg->setFrom('test@gmail.com');
        //now you can access mailer service like that
        $this->mailerService->send($msg);
    }
}

当然,您需要修改在Service Container中配置此服务的方式。

你现在可能有这样的事情:

services:
    your_mailer:
        class:  TestBundle\UserBundle\Utilities\EmailServiceClass

现在您需要添加arguments行以传递依赖项:

services:
    your_mailer:
        class:  TestBundle\UserBundle\Utilities\EmailServiceClass
        arguments:    ['@mailer']

最后一行定义了将传递给服务构造函数的参数。 mailerSwift_Mailer服务的名称。

有关如何管理服务依赖关系的更多详细信息,请参阅Symfony's Book

答案 1 :(得分:1)

请不要使用服务类扩展Controller类。您应该使用services.yml注入所需的依赖项。请实施您的服务:

MyController.php:

activate

EmailServiceClass.php

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MyController extends Controller
{
    public function sendEmailAction()
    {
        $subject = //..
        $to = //..
        $body = //..

        $this->get('email_service.class')->sendEmail($subject, $to, $body);

        // Return a template, or redirect here..
        return new Response();
    }
}

应用程序/配置/ services.yml

class EmailServiceClass
{
    private $mailer;

    public function __construct(\Swift_Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function sendEmail($subject, $to, $body)
    {
        $msg = \Swift_Message::newInstance();

        $msg->setSubject($subject);
        $msg->setTo($to);
        $msg->setBody($body);
        $msg->setContentType('text/html');
        $msg->setCharset('utf-8');
        $msg->setFrom('test@gmail.com');

        $this->mailer->send($msg);
    }
}