@@ -218,8 +218,49 @@ You can do this by defining a new HTML sanitizer in the configuration:
218
218
);
219
219
220
220
This configuration defines a new ``html_sanitizer.sanitizer.app.post_sanitizer ``
221
- service. This service will be :doc: `autowired </service_container/autowiring >`
222
- for services having an ``HtmlSanitizerInterface $appPostSanitizer `` parameter.
221
+ service. Now you have two ways of injecting it in any service or controller:
222
+
223
+ **(1) Use a specific argument name **
224
+
225
+ Type-hint your construtor/method argument with ``HtmlSanitizerInterface `` and name
226
+ the argument using this pattern: "HTML sanitizer name in camelCase". For example, to
227
+ inject the ``app.post_sanitizer `` defined earlier, use an argument named ``$appPostSanitizer ``::
228
+
229
+ // src/Controller/ApiController.php
230
+ namespace App\Controller;
231
+
232
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
233
+ use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface;
234
+
235
+ class BlogController extends AbstractController
236
+ {
237
+ public function __construct(
238
+ private HtmlSanitizerInterface $appPostSanitizer,
239
+ ) {
240
+ }
241
+
242
+ // ...
243
+ }
244
+
245
+ **(2) Use the ``#[Target]`` attribute **
246
+
247
+ When :ref: `dealing with multiple implementations of the same type <autowiring-multiple-implementations-same-type >`
248
+ the ``#[Target] `` attribute helps you select which one to inject. Symfony creates
249
+ a target with the same name as the HTML sanitizer::
250
+
251
+ // ...
252
+ use Symfony\Component\DependencyInjection\Attribute\Target;
253
+
254
+ class BlogController extends AbstractController
255
+ {
256
+ public function __construct(
257
+ #[Target('app.post_sanitizer')]
258
+ private HtmlSanitizerInterface $sanitizer,
259
+ ) {
260
+ }
261
+
262
+ // ...
263
+ }
223
264
224
265
Allow Element Baselines
225
266
~~~~~~~~~~~~~~~~~~~~~~~
0 commit comments