Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 171f4f5

Browse filesBrowse files
committed
minor #20958 [DependencyInjection] Tell about #[AutowireInline] (nicolas-grekas)
This PR was merged into the 7.2 branch. Discussion ---------- [DependencyInjection] Tell about `#[AutowireInline]` I feel like the doc proposed in #19888 is a bit too complex so I'd like to propose this simpler alternative. Commits ------- 849acde [DI] Tell about #[AutowireInline]
2 parents d90c725 + 849acde commit 171f4f5
Copy full SHA for 171f4f5

File tree

Expand file treeCollapse file tree

1 file changed

+32
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+32
-0
lines changed

‎service_container/autowiring.rst

Copy file name to clipboardExpand all lines: service_container/autowiring.rst
+32Lines changed: 32 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.