@@ -148,6 +148,11 @@ that's available to you with or without the use of the base
148
148
action is to look in the
149
149
:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` class.
150
150
151
+ .. tip ::
152
+ If you know what you're doing, you can alternatively extend
153
+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ AbstractController `. It
154
+ has all the same shortcuts, but does not have a ```$this->container `` property.
155
+
151
156
.. index ::
152
157
single: Controller; Redirecting
153
158
@@ -236,12 +241,11 @@ The Symfony templating system and Twig are explained more in the
236
241
Accessing other Services
237
242
~~~~~~~~~~~~~~~~~~~~~~~~
238
243
239
- Symfony comes packed with a lot of useful objects, called *services *. These
240
- are used for rendering templates, sending emails, querying the database and
241
- any other "work" you can think of. When you install a new bundle, it probably
242
- brings in even *more * services.
244
+ Symfony comes packed with a lot of useful objects, called :doc: `services </service_container >`.
245
+ These are used for rendering templates, sending emails, querying the database and
246
+ any other "work" you can think of.
243
247
244
- When extending the base controller class, you can access any Symfony service
248
+ When extending the base `` Controller `` class, you can access any Symfony service
245
249
via the :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::get `
246
250
method of the ``Controller `` class. Here are several common services you might
247
251
need::
@@ -252,6 +256,9 @@ need::
252
256
253
257
$mailer = $this->get('mailer');
254
258
259
+ // you can also fetch parameters
260
+ $someParameter = $this->getParameter('some_parameter');
261
+
255
262
What other services exist? To list all services, use the ``debug:container ``
256
263
console command:
257
264
@@ -261,14 +268,31 @@ console command:
261
268
262
269
For more information, see the :doc: `/service_container ` article.
263
270
264
- .. tip ::
271
+ Services as Controller Arguments
272
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
273
+
274
+ You can also tell Symfony to pass your a service as a controller argument by type-hinting
275
+ it::
265
276
266
- To get a :ref: `container configuration parameter <config-parameter-intro >`,
267
- use the
268
- :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::getParameter `
269
- method::
277
+ use Psr\Log\LoggerInterface
278
+ // ...
279
+
280
+ /**
281
+ * @Route("/lucky/number/{max}")
282
+ */
283
+ public function numberAction($max, LoggerInterface $logger)
284
+ {
285
+ $logger->info('We are logging!');
270
286
271
- $from = $this->getParameter('app.mailer.from');
287
+ // ...
288
+ }
289
+
290
+ .. note ::
291
+ If this isn't working, make sure your controller is registered as a service,
292
+ :ref: `autoconfigured <services-autoconfigure >` and extends either
293
+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` or
294
+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ AbstractController `. Or,
295
+ you can tag it manually with ``controller.service_arguments ``.
272
296
273
297
.. index ::
274
298
single: Controller; Managing errors
@@ -407,20 +431,6 @@ For example, imagine you're processing a :doc:`form </forms>` submission::
407
431
return $this->render(...);
408
432
}
409
433
410
- .. tip ::
411
-
412
- As a developer, you might prefer not to extend the ``Controller ``. To
413
- use the flash message functionality, you can request the flash bag from
414
- the :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session `::
415
-
416
- use Symfony\Component\HttpFoundation\Session\Session;
417
-
418
- public function indexAction(Session $session)
419
- {
420
- // getFlashBag is not available in the SessionInterface and requires the Session
421
- $flashBag = $session->getFlashBag();
422
- }
423
-
424
434
After processing the request, the controller sets a flash message in the session
425
435
and then redirects. The message key (``notice `` in this example) can be anything:
426
436
you'll use this key to retrieve the message.
0 commit comments