Skip to content

Commit d8bbdd7

Browse files
committed
minor #10754 inject to the method instead of using the container (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- inject to the method instead of using the container #SymfonyConHackDay2018 Commits ------- c7b3111 inject to the method instead of using the container
2 parents 9e938db + c7b3111 commit d8bbdd7

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

service_container/autowiring.rst

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ If you're using the :ref:`default services.yml configuration <service-container-
6262
This means you can use them immediately without *any* configuration.
6363

6464
However, to understand autowiring better, the following examples explicitly configure
65-
both services. Also, to keep things simple, configure ``TwitterClient`` to be a
66-
:ref:`public <container-public>` service:
65+
both services:
6766

6867
.. configuration-block::
6968

@@ -79,8 +78,6 @@ both services. Also, to keep things simple, configure ``TwitterClient`` to be a
7978
AppBundle\Service\TwitterClient:
8079
# redundant thanks to _defaults, but value is overridable on each service
8180
autowire: true
82-
# not required, will help in our example
83-
public: true
8481
8582
AppBundle\Util\Rot13Transformer:
8683
autowire: true
@@ -96,7 +93,7 @@ both services. Also, to keep things simple, configure ``TwitterClient`` to be a
9693
<defaults autowire="true" autoconfigure="true" public="false" />
9794
<!-- ... -->
9895
99-
<service id="AppBundle\Service\TwitterClient" autowire="true" public="true" />
96+
<service id="AppBundle\Service\TwitterClient" autowire="true" />
10097
10198
<service id="AppBundle\Util\Rot13Transformer" autowire="true" />
10299
</services>
@@ -111,8 +108,7 @@ both services. Also, to keep things simple, configure ``TwitterClient`` to be a
111108
112109
// the autowire method is new in Symfony 3.3
113110
// in earlier versions, use register() and then call setAutowired(true)
114-
$container->autowire(TwitterClient::class)
115-
->setPublic(true);
111+
$container->autowire(TwitterClient::class);
116112
117113
$container->autowire(Rot13Transformer::class)
118114
->setPublic(false);
@@ -130,11 +126,10 @@ Now, you can use the ``TwitterClient`` service immediately in a controller::
130126
/**
131127
* @Route("/tweet", methods={"POST"})
132128
*/
133-
public function tweetAction()
129+
public function tweetAction(TwitterClient $twitterClient)
134130
{
135131
// fetch $user, $key, $status from the POST'ed data
136132

137-
$twitterClient = $this->container->get(TwitterClient::class);
138133
$twitterClient->tweet($user, $key, $status);
139134

140135
// ...

0 commit comments

Comments
 (0)