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.xmlbundlesResources/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次の行を含める必要がありますloadYourBundle/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 に答える