@@ -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,9 @@ 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
+ For example, suppose you have a simple PHP class that delivers email messages.
73
+ Without a service container, you must manually create the object whenever
74
+ you need it:
75
75
76
76
.. code-block :: php
77
77
@@ -82,11 +82,11 @@ we need it:
82
82
83
83
This is easy enough. The imaginary ``Mailer `` class allows us to configure
84
84
the method used to deliver the email messages (e.g. ``sendmail ``, ``smtp ``, etc).
85
- But what if we wanted to use the mailer service somewhere else? We certainly
86
- don't want to repeat the mailer configuration *every * time we need to use
87
- the ``Mailer `` object. What if we needed to change the ``transport `` from
88
- ``sendmail `` to ``smtp `` everywhere in the application? We 'd need to hunt
89
- down every place we create a ``Mailer `` service and change it.
85
+ But what if you wanted to use the mailer service somewhere else? You certainly
86
+ don't want to repeat the mailer configuration *every * time you need to use
87
+ the ``Mailer `` object. What if you needed to change the ``transport `` from
88
+ ``sendmail `` to ``smtp `` everywhere in the application? You 'd need to hunt
89
+ down every place you create a ``Mailer `` service and change it.
90
90
91
91
.. index ::
92
92
single: Service Container; Configuring services
@@ -95,7 +95,7 @@ Creating/Configuring Services in the Container
95
95
----------------------------------------------
96
96
97
97
A better answer is to let the service container create the ``Mailer `` object
98
- for you. In order for this to work, we must *teach * the container how to
98
+ for you. In order for this to work, you must *teach * the container how to
99
99
create the ``Mailer `` service. This is done via configuration, which can
100
100
be specified in YAML, XML or PHP:
101
101
@@ -154,7 +154,7 @@ shortcut method::
154
154
}
155
155
}
156
156
157
- When we ask for the ``my_mailer `` service from the container, the container
157
+ When you ask for the ``my_mailer `` service from the container, the container
158
158
constructs the object and returns it. This is another major advantage of
159
159
using the service container. Namely, a service is *never * constructed until
160
160
it's needed. If you define a service and never use it on a request, the service
@@ -164,7 +164,7 @@ lots of services. Services that are never used are never constructed.
164
164
165
165
As an added bonus, the ``Mailer `` service is only created once and the same
166
166
instance is returned each time you ask for the service. This is almost always
167
- the behavior you'll need (it's more flexible and powerful), but we 'll learn
167
+ the behavior you'll need (it's more flexible and powerful), but you 'll learn
168
168
later how you can configure a service that has multiple instances.
169
169
170
170
.. _book-service-container-parameters :
@@ -217,16 +217,16 @@ straightforward. Parameters make defining services more organized and flexible:
217
217
));
218
218
219
219
The end result is exactly the same as before - the difference is only in
220
- *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
221
221
``my_mailer.transport `` strings in percent (``% ``) signs, the container knows
222
222
to look for parameters with those names. When the container is built, it
223
223
looks up the value of each parameter and uses it in the service definition.
224
224
225
225
.. note ::
226
226
227
- 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
228
228
be escaped with another percent sign:
229
-
229
+
230
230
.. code-block :: xml
231
231
232
232
<argument type =" string" >http://symfony.com/?foo=%%s& bar=%%d</argument >
@@ -240,7 +240,7 @@ Parameters, however, have several advantages:
240
240
241
241
* parameter values can be used in multiple service definitions;
242
242
243
- * 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
244
244
allows the service to be easily customized in your application.
245
245
246
246
The choice of using or not using parameters is up to you. High-quality
@@ -311,7 +311,7 @@ Importing other Container Configuration Resources
311
311
312
312
.. tip ::
313
313
314
- In this section, we'll refer to service configuration files as *resources *.
314
+ In this section, service configuration files are referred to as *resources *.
315
315
This is to highlight that fact that, while most configuration resources
316
316
will be files (e.g. YAML, XML, PHP), Symfony2 is so flexible that configuration
317
317
could be loaded from anywhere (e.g. a database or even via an external
@@ -323,9 +323,9 @@ The service container is built using a single configuration resource
323
323
be imported from inside this file in one way or another. This gives you absolute
324
324
flexibility over the services in your application.
325
325
326
- External service configuration can be imported in two different ways. First,
327
- we'll talk about the method that you'll use most commonly in your application:
328
- 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
329
329
second method, which is the flexible and preferred method for importing service
330
330
configuration from third-party bundles.
331
331
@@ -337,7 +337,7 @@ configuration from third-party bundles.
337
337
Importing Configuration with ``imports ``
338
338
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
339
339
340
- 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
341
341
in the application configuration file (e.g. ``app/config/config.yml ``). Of
342
342
course, since the ``Mailer `` class itself lives inside the ``AcmeHelloBundle ``,
343
343
it makes more sense to put the ``my_mailer `` container definition inside the
@@ -389,7 +389,7 @@ directories don't exist, create them.
389
389
));
390
390
391
391
The definition itself hasn't changed, only its location. Of course the service
392
- 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
393
393
easily import the resource file using the ``imports `` key in the application
394
394
configuration.
395
395
@@ -436,7 +436,7 @@ Symfony2 core services, are usually loaded using another method that's more
436
436
flexible and easy to configure in your application.
437
437
438
438
Here's how it works. Internally, each bundle defines its services very much
439
- 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
440
440
resource files (usually XML) to specify the parameters and services for that
441
441
bundle. However, instead of importing each of these resources directly from
442
442
your application configuration using the ``imports `` directive, you can simply
@@ -452,7 +452,7 @@ to accomplish two things:
452
452
service container configuration.
453
453
454
454
In other words, a service container extension configures the services for
455
- 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
456
456
a sensible, high-level interface for configuring the bundle.
457
457
458
458
Take the ``FrameworkBundle `` - the core Symfony2 framework bundle - as an
@@ -541,10 +541,10 @@ in its constructor, which is easily configurable. As you'll see, the real
541
541
power of the container is realized when you need to create a service that
542
542
depends on one or more other services in the container.
543
543
544
- 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 ``,
545
545
that helps to manage the preparation and delivery of an email message to
546
546
a collection of addresses. Of course the ``my_mailer `` service is already
547
- 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 ``
548
548
to handle the actual delivery of the messages. This pretend class might look
549
549
something like this::
550
550
@@ -565,7 +565,7 @@ something like this::
565
565
// ...
566
566
}
567
567
568
- Without using the service container, we can create a new ``NewsletterManager ``
568
+ Without using the service container, you can create a new ``NewsletterManager ``
569
569
fairly easily from inside a controller::
570
570
571
571
public function sendNewsletterAction()
@@ -575,8 +575,8 @@ fairly easily from inside a controller::
575
575
// ...
576
576
}
577
577
578
- This approach is fine, but what if we decide later that the ``NewsletterManager ``
579
- 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
580
580
refactor our code and rename the class? In both cases, you'd need to find every
581
581
place where the ``NewsletterManager `` is instantiated and modify it. Of course,
582
582
the service container gives us a much more appealing option:
@@ -812,7 +812,7 @@ In Symfony2, you'll constantly use services provided by the Symfony core or
812
812
other third-party bundles to perform tasks such as rendering templates (``templating ``),
813
813
sending emails (``mailer ``), or accessing information on the request (``request ``).
814
814
815
- We can take this a step further by using these services inside services that
815
+ You can take this a step further by using these services inside services that
816
816
you've created for your application. Let's modify the ``NewsletterManager ``
817
817
to use the real Symfony2 ``mailer `` service (instead of the pretend ``my_mailer ``).
818
818
Let's also pass the templating engine service to the ``NewsletterManager ``
@@ -873,7 +873,7 @@ the framework.
873
873
.. tip ::
874
874
875
875
Be sure that ``swiftmailer `` entry appears in your application
876
- configuration. As we mentioned in :ref: `service-container-extension-configuration `,
876
+ configuration. As you mentioned in :ref: `service-container-extension-configuration `,
877
877
the ``swiftmailer `` key invokes the service extension from the
878
878
``SwiftmailerBundle ``, which registers the ``mailer `` service.
879
879
0 commit comments