Skip to content

Commit d16042c

Browse files
[DI] Tell about #[AutowireInline]
1 parent 32fab7e commit d16042c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

service_container/autowiring.rst

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

0 commit comments

Comments
 (0)