@@ -12,12 +12,12 @@ your product inventory, or another object that processes data from a third-party
12
12
API. The point is that a modern application does many things and is organized
13
13
into many objects that handle each task.
14
14
15
- In this chapter, we'll talk about a special PHP object in Symfony2 that helps
15
+ This chapter is about a special PHP object in Symfony2 that helps
16
16
you instantiate, organize and retrieve the many objects of your application.
17
17
This object, called a service container, will allow you to standardize and
18
18
centralize the way objects are constructed in your application. The container
19
19
makes your life easier, is super fast, and emphasizes an architecture that
20
- promotes reusable and decoupled code. And since all core Symfony2 classes
20
+ promotes reusable and decoupled code. Since all core Symfony2 classes
21
21
use the container, you'll learn how to extend, configure and use any object
22
22
in Symfony2. In large part, the service container is the biggest contributor
23
23
to the speed and extensibility of Symfony2.
@@ -69,9 +69,10 @@ What is a Service Container?
69
69
70
70
A :term: `Service Container ` (or *dependency injection container *) is simply
71
71
a PHP object that manages the instantiation of services (i.e. objects).
72
- For example, suppose we have a simple PHP class that delivers email messages.
73
- Without a service container, we must manually create the object whenever
74
- we need it::
72
+
73
+ For example, suppose you have a simple PHP class that delivers email messages.
74
+ Without a service container, you must manually create the object whenever
75
+ you need it::
75
76
76
77
use Acme\HelloBundle\Mailer;
77
78
@@ -80,11 +81,11 @@ we need it::
80
81
81
82
This is easy enough. The imaginary ``Mailer `` class allows us to configure
82
83
the method used to deliver the email messages (e.g. ``sendmail ``, ``smtp ``, etc).
83
- But what if we wanted to use the mailer service somewhere else? We certainly
84
- don't want to repeat the mailer configuration *every * time we need to use
85
- the ``Mailer `` object. What if we needed to change the ``transport `` from
86
- ``sendmail `` to ``smtp `` everywhere in the application? We 'd need to hunt
87
- down every place we create a ``Mailer `` service and change it.
84
+ But what if you wanted to use the mailer service somewhere else? You certainly
85
+ don't want to repeat the mailer configuration *every * time you need to use
86
+ the ``Mailer `` object. What if you needed to change the ``transport `` from
87
+ ``sendmail `` to ``smtp `` everywhere in the application? You 'd need to hunt
88
+ down every place you create a ``Mailer `` service and change it.
88
89
89
90
.. index ::
90
91
single: Service Container; Configuring services
@@ -93,7 +94,7 @@ Creating/Configuring Services in the Container
93
94
----------------------------------------------
94
95
95
96
A better answer is to let the service container create the ``Mailer `` object
96
- for you. In order for this to work, we must *teach * the container how to
97
+ for you. In order for this to work, you must *teach * the container how to
97
98
create the ``Mailer `` service. This is done via configuration, which can
98
99
be specified in YAML, XML or PHP:
99
100
@@ -152,7 +153,7 @@ shortcut method::
152
153
}
153
154
}
154
155
155
- When we ask for the ``my_mailer `` service from the container, the container
156
+ When you ask for the ``my_mailer `` service from the container, the container
156
157
constructs the object and returns it. This is another major advantage of
157
158
using the service container. Namely, a service is *never * constructed until
158
159
it's needed. If you define a service and never use it on a request, the service
@@ -162,8 +163,8 @@ lots of services. Services that are never used are never constructed.
162
163
163
164
As an added bonus, the ``Mailer `` service is only created once and the same
164
165
instance is returned each time you ask for the service. This is almost always
165
- the behavior you'll need (it's more flexible and powerful), but you learn how
166
- to configure a service that has multiple instances in the
166
+ the behavior you'll need (it's more flexible and powerful), but you'll learn
167
+ later how you can configure a service that has multiple instances in the
167
168
":doc: `/cookbook/service_container/scopes `" cookbook article.
168
169
169
170
.. _book-service-container-parameters :
@@ -216,16 +217,16 @@ straightforward. Parameters make defining services more organized and flexible:
216
217
));
217
218
218
219
The end result is exactly the same as before - the difference is only in
219
- *how * we defined the service. By surrounding the ``my_mailer.class `` and
220
+ *how * you defined the service. By surrounding the ``my_mailer.class `` and
220
221
``my_mailer.transport `` strings in percent (``% ``) signs, the container knows
221
222
to look for parameters with those names. When the container is built, it
222
223
looks up the value of each parameter and uses it in the service definition.
223
224
224
225
.. note ::
225
226
226
- The percent sign inside a parameter or argument, as part of the string, must
227
+ The percent sign inside a parameter or argument, as part of the string, must
227
228
be escaped with another percent sign:
228
-
229
+
229
230
.. code-block :: xml
230
231
231
232
<argument type =" string" >http://symfony.com/?foo=%%s& bar=%%d</argument >
@@ -239,7 +240,7 @@ Parameters, however, have several advantages:
239
240
240
241
* parameter values can be used in multiple service definitions;
241
242
242
- * when creating a service in a bundle (we'll show this shortly), using parameters
243
+ * when creating a service in a bundle (this follows shortly), using parameters
243
244
allows the service to be easily customized in your application.
244
245
245
246
The choice of using or not using parameters is up to you. High-quality
@@ -310,7 +311,7 @@ Importing other Container Configuration Resources
310
311
311
312
.. tip ::
312
313
313
- In this section, we'll refer to service configuration files as *resources *.
314
+ In this section, service configuration files are referred to as *resources *.
314
315
This is to highlight that fact that, while most configuration resources
315
316
will be files (e.g. YAML, XML, PHP), Symfony2 is so flexible that configuration
316
317
could be loaded from anywhere (e.g. a database or even via an external
@@ -322,9 +323,9 @@ The service container is built using a single configuration resource
322
323
be imported from inside this file in one way or another. This gives you absolute
323
324
flexibility over the services in your application.
324
325
325
- External service configuration can be imported in two different ways. First,
326
- we'll talk about the method that you'll use most commonly in your application:
327
- the ``imports `` directive. In the following section, we'll introduce the
326
+ External service configuration can be imported in two different ways. First up is
327
+ the method that you'll use most commonly in your application:
328
+ the ``imports `` directive. The section after introduces the
328
329
second method, which is the flexible and preferred method for importing service
329
330
configuration from third-party bundles.
330
331
@@ -336,7 +337,7 @@ configuration from third-party bundles.
336
337
Importing Configuration with ``imports ``
337
338
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
338
339
339
- So far, we 've placed our ``my_mailer `` service container definition directly
340
+ So far, you 've placed our ``my_mailer `` service container definition directly
340
341
in the application configuration file (e.g. ``app/config/config.yml ``). Of
341
342
course, since the ``Mailer `` class itself lives inside the ``AcmeHelloBundle ``,
342
343
it makes more sense to put the ``my_mailer `` container definition inside the
@@ -388,7 +389,7 @@ directories don't exist, create them.
388
389
));
389
390
390
391
The definition itself hasn't changed, only its location. Of course the service
391
- container doesn't know about the new resource file. Fortunately, we can
392
+ container doesn't know about the new resource file. Fortunately, you can
392
393
easily import the resource file using the ``imports `` key in the application
393
394
configuration.
394
395
@@ -435,7 +436,7 @@ Symfony2 core services, are usually loaded using another method that's more
435
436
flexible and easy to configure in your application.
436
437
437
438
Here's how it works. Internally, each bundle defines its services very much
438
- like we 've seen so far. Namely, a bundle uses one or more configuration
439
+ like you 've seen so far. Namely, a bundle uses one or more configuration
439
440
resource files (usually XML) to specify the parameters and services for that
440
441
bundle. However, instead of importing each of these resources directly from
441
442
your application configuration using the ``imports `` directive, you can simply
@@ -451,7 +452,7 @@ to accomplish two things:
451
452
service container configuration.
452
453
453
454
In other words, a service container extension configures the services for
454
- a bundle on your behalf. And as we 'll see in a moment, the extension provides
455
+ a bundle on your behalf. And as you 'll see in a moment, the extension provides
455
456
a sensible, high-level interface for configuring the bundle.
456
457
457
458
Take the ``FrameworkBundle `` - the core Symfony2 framework bundle - as an
@@ -540,10 +541,10 @@ in its constructor, which is easily configurable. As you'll see, the real
540
541
power of the container is realized when you need to create a service that
541
542
depends on one or more other services in the container.
542
543
543
- Let's start with an example. Suppose we have a new service, ``NewsletterManager ``,
544
+ Let's start with an example. Suppose you have a new service, ``NewsletterManager ``,
544
545
that helps to manage the preparation and delivery of an email message to
545
546
a collection of addresses. Of course the ``my_mailer `` service is already
546
- really good at delivering email messages, so we 'll use it inside ``NewsletterManager ``
547
+ really good at delivering email messages, so you 'll use it inside ``NewsletterManager ``
547
548
to handle the actual delivery of the messages. This pretend class might look
548
549
something like this::
549
550
@@ -564,7 +565,7 @@ something like this::
564
565
// ...
565
566
}
566
567
567
- Without using the service container, we can create a new ``NewsletterManager ``
568
+ Without using the service container, you can create a new ``NewsletterManager ``
568
569
fairly easily from inside a controller::
569
570
570
571
public function sendNewsletterAction()
@@ -574,8 +575,8 @@ fairly easily from inside a controller::
574
575
// ...
575
576
}
576
577
577
- This approach is fine, but what if we decide later that the ``NewsletterManager ``
578
- class needs a second or third constructor argument? What if we decide to
578
+ This approach is fine, but what if you decide later that the ``NewsletterManager ``
579
+ class needs a second or third constructor argument? What if you decide to
579
580
refactor our code and rename the class? In both cases, you'd need to find every
580
581
place where the ``NewsletterManager `` is instantiated and modify it. Of course,
581
582
the service container gives us a much more appealing option:
@@ -809,7 +810,7 @@ In Symfony2, you'll constantly use services provided by the Symfony core or
809
810
other third-party bundles to perform tasks such as rendering templates (``templating ``),
810
811
sending emails (``mailer ``), or accessing information on the request (``request ``).
811
812
812
- We can take this a step further by using these services inside services that
813
+ You can take this a step further by using these services inside services that
813
814
you've created for your application. Let's modify the ``NewsletterManager ``
814
815
to use the real Symfony2 ``mailer `` service (instead of the pretend ``my_mailer ``).
815
816
Let's also pass the templating engine service to the ``NewsletterManager ``
@@ -870,7 +871,7 @@ the framework.
870
871
.. tip ::
871
872
872
873
Be sure that ``swiftmailer `` entry appears in your application
873
- configuration. As we mentioned in :ref: `service-container-extension-configuration `,
874
+ configuration. As you mentioned in :ref: `service-container-extension-configuration `,
874
875
the ``swiftmailer `` key invokes the service extension from the
875
876
``SwiftmailerBundle ``, which registers the ``mailer `` service.
876
877
0 commit comments