@@ -159,6 +159,11 @@ controller classes. A great way to see the core functionality in
159
159
action is to look in the
160
160
:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` class.
161
161
162
+ .. tip ::
163
+ If you know what you're doing, you can alternatively extend
164
+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ AbstractController `. It
165
+ has all the same shortcuts, but does not have a ```$this->container `` property.
166
+
162
167
.. index ::
163
168
single: Controller; Redirecting
164
169
@@ -247,10 +252,9 @@ The Symfony templating system and Twig are explained more in the
247
252
Accessing other Services
248
253
~~~~~~~~~~~~~~~~~~~~~~~~
249
254
250
- Symfony comes packed with a lot of useful objects, called *services *. These
251
- are used for rendering templates, sending emails, querying the database and
252
- any other "work" you can think of. When you install a new bundle, it probably
253
- brings in even *more * services.
255
+ Symfony comes packed with a lot of useful objects, called :doc: `services </service_container >`.
256
+ These are used for rendering templates, sending emails, querying the database and
257
+ any other "work" you can think of.
254
258
255
259
When extending the base ``Controller `` class, you can access any Symfony service
256
260
via the :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::get `
@@ -262,6 +266,9 @@ method. Here are several common services you might need::
262
266
263
267
$mailer = $this->get('mailer');
264
268
269
+ // you can also fetch parameters
270
+ $someParameter = $this->getParameter('some_parameter');
271
+
265
272
What other services exist? To list all services, use the ``debug:container ``
266
273
console command:
267
274
@@ -271,14 +278,31 @@ console command:
271
278
272
279
For more information, see the :doc: `/service_container ` article.
273
280
274
- .. tip ::
281
+ Services as Controller Arguments
282
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
283
+
284
+ You can also tell Symfony to pass your a service as a controller argument by type-hinting
285
+ it::
275
286
276
- To get a :ref: `container configuration parameter <config-parameter-intro >`,
277
- use the
278
- :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::getParameter `
279
- method::
287
+ use Psr\Log\LoggerInterface
288
+ // ...
289
+
290
+ /**
291
+ * @Route("/lucky/number/{max}")
292
+ */
293
+ public function numberAction($max, LoggerInterface $logger)
294
+ {
295
+ $logger->info('We are logging!');
280
296
281
- $from = $this->getParameter('app.mailer.from');
297
+ // ...
298
+ }
299
+
300
+ .. note ::
301
+ If this isn't working, make sure your controller is registered as a service,
302
+ :ref: `autoconfigured <services-autoconfigure >` and extends either
303
+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` or
304
+ :class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ AbstractController `. Or,
305
+ you can tag it manually with ``controller.service_arguments ``.
282
306
283
307
.. index ::
284
308
single: Controller; Managing errors
@@ -418,20 +442,6 @@ For example, imagine you're processing a :doc:`form </forms>` submission::
418
442
return $this->render(...);
419
443
}
420
444
421
- .. tip ::
422
-
423
- As a developer, you might prefer not to extend the ``Controller ``. To
424
- use the flash message functionality, you can request the flash bag from
425
- the :class: `Symfony\\ Component\\ HttpFoundation\\ Session\\ Session `::
426
-
427
- use Symfony\Component\HttpFoundation\Session\Session;
428
-
429
- public function indexAction(Session $session)
430
- {
431
- // getFlashBag is not available in the SessionInterface and requires the Session
432
- $flashBag = $session->getFlashBag();
433
- }
434
-
435
445
After processing the request, the controller sets a flash message in the session
436
446
and then redirects. The message key (``notice `` in this example) can be anything:
437
447
you'll use this key to retrieve the message.
0 commit comments