Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this by <a href="http://symfony.com/doc/current/book/service_container.html#referencing-injecting-services" rel="nofollow">"Referencing (Injecting) Services"</a>. In your case you want to inject the Request which is a <a href="http://symfony.com/doc/current/cookbook/service_container/scopes.html#using-a-service-from-a-narrower-scope" rel="nofollow">service from a narrower scope</a>. </p> <p>If you are using transformers, you are probably already using a Custom Form Type, and are instantiating the Data Transformer within your Form Type <strong>BuildForm</strong> Method, see <a href="http://symfony.com/doc/current/cookbook/form/data_transformers.html#using-the-transformer" rel="nofollow">here</a> for more info. </p> <p>You want to inject the Request object to the custom Form Type, then it can passed to the Data Transformer as a constructor parameter. </p> <p>To do this modify the <code>services.yml</code> file with in your bundle, and add a constructor to the Custom Form Type and the Custom Data Transformer like this:</p> <pre><code>// 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 } </code></pre> <p>The update the CustomType Class like this:</p> <pre><code>&lt;?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-&gt;dm = $dm; $this-&gt;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-&gt;dm, $this-&gt;request); $builder-&gt;addModelTransformer($transformer); } // ... } </code></pre> <p>and finally add a constructor to the transformer similar to the one added in the Form Type:</p> <pre><code>&lt;?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-&gt;dm = $dm; $this-&gt;request = $request; } // ... } </code></pre> <p>Notice that along with the Request I have injected the MongoDB DocumentManager, this is to show that multiple objects can be injected.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload