Skip to content

Commit 33647a6

Browse files
committed
Tweaks after review!
1 parent adadb5a commit 33647a6

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

best_practices/business-logic.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Next, define a new service for that class.
8686
services:
8787
# ...
8888
89-
# use the class name as the service id
89+
# use the fully-qualified class name as the service id
9090
AppBundle\Utils\Slugger:
9191
public: false
9292
@@ -116,7 +116,8 @@ Now you can use the custom slugger in any controller class, such as the
116116
{
117117
// ...
118118
119-
// you can also fetch a public service like this, but is not the best-practice
119+
// you can also fetch a public service like this
120+
// but fetching services in this way is not considered a best practice
120121
// $slugger = $this->get('app.slugger');
121122
122123
if ($form->isSubmitted() && $form->isValid()) {

best_practices/controllers.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Fetching Services
119119
-----------------
120120

121121
If you extend the base ``Controller`` class, you can access services directly from
122-
the container via ``$this->container->get()`` or ``$this->get()``. Instead, you
122+
the container via ``$this->container->get()`` or ``$this->get()``. But instead, you
123123
should use dependency injection to fetch services: most easily done by
124124
:ref:`type-hinting action method arguments <controller-accessing-services>`:
125125

@@ -128,6 +128,9 @@ should use dependency injection to fetch services: most easily done by
128128
Don't use ``$this->get()`` or ``$this->container->get()`` to fetch services
129129
from the container. Instead, use dependency injection.
130130

131+
By not fetching services directly from the container, you can make your services
132+
*private*, which has :ref:`several advantages <services-why-private>`.
133+
131134
.. _best-practices-paramconverter:
132135

133136
Using the ParamConverter
@@ -177,14 +180,13 @@ manually. In our application, we have this situation in ``CommentController``:
177180

178181
.. code-block:: php
179182
180-
use Doctrine\ORM\EntityManagerInterface;
181-
182183
/**
183184
* @Route("/comment/{postSlug}/new", name = "comment_new")
184185
*/
185-
public function newAction(Request $request, $postSlug, EntityManagerInterface $em)
186+
public function newAction(Request $request, $postSlug)
186187
{
187-
$post = $em->getRepository('AppBundle:Post')
188+
$post = $this->getDoctrine()
189+
->getRepository('AppBundle:Post')
188190
->findOneBy(array('slug' => $postSlug));
189191
190192
if (!$post) {

service_container/alias_private.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,13 @@ You can also control the ``public`` option on a service-by-service basis:
5757
$container->register(Foo::class)
5858
->setPublic(false);
5959
60+
.. _services-why-private:
61+
6062
Private services are special because they allow the container to optimize whether
61-
and how they are instantiated. This increases the container's performance.
63+
and how they are instantiated. This increases the container's performance. It also
64+
gives you better errors: if you try to reference a non-existent service, you will
65+
get a clear error when you refresh *any* page, even if the problematic code would
66+
not have run on that page.
6267

6368
Now that the service is private, you *should not* fetch the service directly
6469
from the container::

0 commit comments

Comments
 (0)