TwigSwiftMailer类只User
向模板公开实体和确认 url。您必须扩展类并修改方法。然后创建服务并设置为默认邮件。您可以在此处查看服务定义。
编辑:
示例实现将是
班上 。
//namespace declaration
class MySwiftMailer extends TwigSwiftMailer
{
private $container;
/**
* @param Symofony\Component\DependencyInjection\ContainerInerface $container
*/
public function setContainer(ContainerInterface $container)
{
$this->container = $container;
}
public function sendConfirmationEmailMessage(UserInterface $user)
{
$template = $this->parameters['template']['confirmation'];
$url = $this->router->generate('fos_user_registration_confirm', array('token' => $user->getConfirmationToken()), true);
$context = array(
'user' => $user,
'container' => $this->container,
'session' => $this->container->get('request')->getSession(), // expose session
'confirmationUrl' => $url
);
$this->sendMessage($template, $context, $this->parameters['from_email']['confirmation'], $user->getEmail());
}
// implement sendResettingEmailMessage() in same way
}
服务声明。mailer.xml
在您的 bundlesResources/config
文件夹中创建一个名为的类。
<?xml version="1.0" encoding="UTF-8"?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="fos_user.mailer.my_swift_mailer" class="FOS\UserBundle\Mailer\TwigSwiftMailer" >
<argument type="service" id="mailer" />
<argument type="service" id="router" />
<argument type="service" id="twig" />
<argument type="collection">
<argument key="template" type="collection">
<argument key="confirmation">%fos_user.registration.confirmation.template%</argument>
<argument key="resetting">%fos_user.resetting.email.template%</argument>
</argument>
<argument key="from_email" type="collection">
<argument key="confirmation">%fos_user.registration.confirmation.from_email%</argument>
<argument key="resetting">%fos_user.resetting.email.from_email%</argument>
</argument>
</argument>
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
</service>
</services>
</container>
要包括loader.xml
你必须在load
方法中包括以下几行YourBundle/DependencyInjection/YourBundleExtension.php
$xmlLoader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$xmlLoader->load("mailer.xml");
并在app/config.yml
设置邮件。
# app/config/config.yml
fos_user:
# ...
service:
mailer: fos_user.mailer.my_swift_mailer
现在在您的模板中,您可以执行{{ session.get('var') }}
或{{ container.getParameter('any_param') }}