从 DataTransformer 类获取请求服务 [英] Get request service from a DataTransformer class

查看:17
本文介绍了从 DataTransformer 类获取请求服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小故事:
我需要从一个不是从 Controller 类继承的类中获取请求服务(它是一个 DataTransformer,它显然实现了 DataTransformerInterface).

Short story:
I need to get the Request service from a class that doesn't inherit from the Controller class (it's a DataTransformer which -obviously- implements the DataTransformerInterface).

长话:
我有一个带有电子邮件字段的嵌入式表单.如果用户输入的电子邮件不存在于我的用户数据库表中,我想使用此电子邮件创建一个新用户.
为了做到这一点,我需要设置它的 IP,所以我遵循了 嵌入式表单教程数据转换器配方,但最后我不知道在哪里可以将 Request 实例注入到我的 DataTransformer 构造函数或其他东西中.

Long story:
I have an embedded form that has an email field. If the user enters an email which doesn't exists in my users database table, I want to create a new user with this email.
In order to do that, I need to set its IP, so I followed the embedded forms tutorial and the data transformer recipe, but finally I have no idea where I'm able to inject the Request instance to my DataTransformer constructor or something else.

如果我在一个从 Controller 扩展的类中,它会很简单:
$this->container->get('request')->getClientIp()

If I was in a class extending form the Controller one, it would be as simple as:
$this->container->get('request')->getClientIp()

推荐答案

您可以通过 "引用(注入)服务".在您的情况下,您想要注入请求,它是一个 范围更窄的服务.

You can do this by "Referencing (Injecting) Services". In your case you want to inject the Request which is a service from a narrower scope.

如果您正在使用转换器,您可能已经在使用自定义表单类型,并且正在您的表单类型中实例化数据转换器 BuildForm 方法,请参阅 此处 了解更多信息.

If you are using transformers, you are probably already using a Custom Form Type, and are instantiating the Data Transformer within your Form Type BuildForm Method, see here for more info.

您想将 Request 对象注入自定义 Form Type,然后它可以作为构造函数参数传递给 Data Transformer.

You want to inject the Request object to the custom Form Type, then it can passed to the Data Transformer as a constructor parameter.

为此修改包中的 services.yml 文件,并向自定义表单类型和自定义数据转换器添加一个构造函数,如下所示:

To do this modify the services.yml file with in your bundle, and add a constructor to the Custom Form Type and the Custom Data Transformer like this:

// src/Acme/HelloBundle/Resources/config/services.yml
parameters:
    // ...    

services:
    acme.type.custom_type:
        class: Acme\HelloBundle\Form\Type\CustomType
        scope: request
        arguments: ["@doctrine.odm.mongodb.document_manager", "@request"]
        tags:
            - { name: form.type, alias: custom_type }

像这样更新 CustomType 类:

The update the CustomType Class like this:

<?php
// src/Acme/HelloBundle/Form/Type/CustomType.php

namespace Acme\HelloBundle\Form\Type;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ODM\MongoDB\DocumentManager;

use Acme\HelloBundle\Form\DataTransformer\CustomDataTransformer;

class CustomType extends AbstractType
{
    private $request;
    private $dm;

    public function __construct(DocumentManager $dm, Request $request) {
        $this->dm = $dm;
        $this->request = $request;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Create a new Data Transformer that will be able to use the Request Object!
        $transformer = new CustomDataTransformer($this->dm, $this->request);
        $builder->addModelTransformer($transformer);
    }
    // ...
}

最后向转换器添加一个构造函数,类似于在表单类型中添加的构造函数:

and finally add a constructor to the transformer similar to the one added in the Form Type:

<?php
// src/Acme/HelloBundle/Form/DataTransformer/CustomDataTransformer.php

namespace Acme\HelloBundle\Form\DataTransformer;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\DataTransformerInterface;
use Doctrine\ODM\MongoDB\DocumentManager;

class CustomDataTransformer implements DataTransformerInterface
{
    private $request;
    private $dm;

    public function __construct(DocumentManager $dm, Request $request) {
        $this->dm = $dm;
        $this->request = $request;
    }

    // ...
}

注意我在Request中注入了MongoDB DocumentManager,这是为了说明可以注入多个对象.

Notice that along with the Request I have injected the MongoDB DocumentManager, this is to show that multiple objects can be injected.

这篇关于从 DataTransformer 类获取请求服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆