Skip to content

Commit a0d627a

Browse files
committed
Merge branch '7.3' into 7.4
* 7.3: [HtmlSanitizer] Improve the explanation about injecting sanitizers in services
2 parents 84fd43a + d3e9861 commit a0d627a

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
@@ -218,8 +218,49 @@ You can do this by defining a new HTML sanitizer in the configuration:
218218
);
219219
220220
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+
}
223264

224265
Allow Element Baselines
225266
~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)