【发布时间】:2016-08-11 12:32:09
【问题描述】:
我尝试在我的 Symfony Sonata 捆绑包中提供一项服务,以便在创建订单后立即向特定人员发送电子邮件。接收电子邮件的人是用户选择批准订单的人。
我尝试关注service container documentation on Symfony's website,但对我来说感觉太不完整了。我想看一个完整的例子,而不仅仅是几个 sn-ps。
这是我目前为止的电子邮件服务课程;
<?php
namespace Qi\Bss\BaseBundle\Lib\PurchaseModule;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Doctrine\ORM\EntityManager;
/**
*
*/
class Notifier
{
/**
* Service container
* @var type
*/
private $serviceContainer;
public function notifier($subject, $from, $to, $body) {
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from)
->setTo($to)
->setBody($body)
;
$this->serviceContainer->get('mailer')->send($message);
}
/**
* Sets the sales order exporter object
* @param type $serviceContainer
*/
public function setServiceContainer($serviceContainer)
{
$this->serviceContainer = $serviceContainer;
}
}
我的 services.yml 文件中的服务如下所示;
bss.pmod.order_notifier:
class: Qi\Bss\BaseBundle\Lib\PurchaseModule\Notifier
arguments: ["@mailer"]
当我在控制器操作中调用服务时,我使用这一行;
$this->get('bss.pmod.order_notifier')->notifier();
我得到的错误状态;
注意:未定义的属性: Qi\Bss\FrontendBundle\Controller\PmodOrderController::$serviceContainer
就像我之前说的,我看过service container documentation,但我看不懂。
有人可以帮我举一个很好的完整例子来解释一切吗?
【问题讨论】: