【问题标题】:FOSUserBundle : access session var in a custom emailFOSUserBundle :在自定义电子邮件中访问会话变量
【发布时间】:2012-04-02 16:54:46
【问题描述】:

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

这是我的代码(类似于 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 访问中,但无法在此模板中访问它们。

非常感谢您的帮助! 一个

【问题讨论】:

    标签: php symfony fosuserbundle


    【解决方案1】:

    TwigSwiftMailer 类只向模板公开User 实体和确认 url。您必须扩展类并修改方法。然后创建服务并设置为默认邮件。服务定义可以查看here

    编辑:

    示例实现将是

    班级。

    //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
    }
    

    服务声明。在你的 bundles Resources/config 文件夹中创建一个名为 mailer.xml 的类。

    <?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,您必须在YourBundle/DependencyInjection/YourBundleExtension.phpload 方法中包含以下行

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

    【讨论】:

    • 嗨,这听起来是个不错的解决方案,但我可以在这个扩展类中访问“会话”或“配置参数”(来自 parameters.ini)吗?
    • 是的,如果你注入@service_container
    • @request 获取当前会话。
    • 哇...非常感谢,我今天要试试这个,如果一切正常,我会回来告诉你!
    • 一切正常!再次非常感谢!
    【解决方案2】:

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

    和app.session一样

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

    【讨论】:

      猜你喜欢
      • 2018-09-29
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 2017-04-22
      • 2016-10-24
      • 2015-03-20
      • 2017-08-06
      相关资源
      最近更新 更多