Skip to content

Commit 7cc3230

Browse files
committed
Merge branch '7.4' into 8.0
* 7.4: [HtmlSanitizer] Improve the explanation about injecting sanitizers in services
2 parents 436d8cc + a0d627a commit 7cc3230

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

html_sanitizer.rst

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

217258
Allow Element Baselines
218259
~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)