Skip to content

Commit 849acde

Browse files
[DI] Tell about #[AutowireInline]
1 parent d90c725 commit 849acde

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

service_container/autowiring.rst

+32
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,38 @@ typed properties:
862862
}
863863
}
864864
865+
Autowiring Anonymous Services Inline
866+
------------------------------------
867+
868+
.. versionadded:: 7.1
869+
870+
The ``#[AutowireInline]`` attribute was added in Symfony 7.1.
871+
872+
Similar to how one can define anonymous services inline using configuration files,
873+
the :class:`Symfony\\Component\\DependencyInjection\\Attribute\\AutowireInline`
874+
attribute allows declaring anonymous services inline next to their corresponding
875+
arguments::
876+
877+
public function __construct(
878+
#[AutowireInline(
879+
factory: [ScopingHttpClient::class, 'forBaseUri'],
880+
arguments: [
881+
'$baseUri' => 'https://api.example.com',
882+
'$defaultOptions' => [
883+
'auth_bearer' => '%env(EXAMPLE_TOKEN)%',
884+
],
885+
]
886+
)]
887+
private HttpClientInterface $client,
888+
) {
889+
}
890+
891+
As you might have already figured out, this declaration instructs Symfony to inject an
892+
object created by calling the ``ScopingHttpClient::forBaseUri()`` factory with the
893+
configured base URI and default options.
894+
895+
Of course, this is just an example and this attribute can be used to construct any kind of objects.
896+
865897
Autowiring Controller Action Methods
866898
------------------------------------
867899

0 commit comments

Comments
 (0)