0

我正在自定义用户注册后发送的确认电子邮件。我的问题是我无法访问电子邮件模板中的会话变量。

这是我的代码(类似于 FOSUser 文档):

{# src/Acme/DemoBundle/Resources/views/User/confirmation.email.twig #}

{% block subject %}Confirmation{% endblock %}

{% block body_text %}
{% autoescape false %}
Hello {{ user.username }} !

Your locale is : {{ app.session.locale }}

Click on the following link to confirm your registration : {{ confirmationUrl }}

Greetings,
the Acme team
{% endautoescape %}
{% endblock %}

{% block body_html %}
{% include 'AcmeDemoBundle:User:confirmation_email.html.twig' %}
{% endblock %}

以下行返回异常:

Your locale is : {{ app.session.locale }}

例外 :

Variable "app" does not exist in ...

如何从此模板访问会话变量?

我还需要访问配置参数(来自 parameters.ini)。我的参数已经在全局 Twig 访问中,但无法在此模板中访问它们。

非常感谢您的帮助 !一种

4

2 回答 2

2

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') }}

于 2012-04-02T18:30:59.643 回答
0

$session = $ this->get("session");

和 app.session 一样

因此,如果您愿意,可以通过 RenderView() 发送 $session

于 2012-04-02T18:41:43.893 回答