@@ -159,6 +159,11 @@ controller classes. A great way to see the core functionality in
159159action is to look in the
160160:class: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller ` class.
161161
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+
162167.. index ::
163168 single: Controller; Redirecting
164169
@@ -247,10 +252,9 @@ The Symfony templating system and Twig are explained more in the
247252Accessing other Services
248253~~~~~~~~~~~~~~~~~~~~~~~~
249254
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.
254258
255259When extending the base ``Controller `` class, you can access any Symfony service
256260via the :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::get `
@@ -262,6 +266,9 @@ method. Here are several common services you might need::
262266
263267 $mailer = $this->get('mailer');
264268
269+ // you can also fetch parameters
270+ $someParameter = $this->getParameter('some_parameter');
271+
265272What other services exist? To list all services, use the ``debug:container ``
266273console command:
267274
@@ -271,14 +278,31 @@ console command:
271278
272279 For more information, see the :doc: `/service_container ` article.
273280
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::
275286
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!');
280296
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 ``.
282306
283307.. index ::
284308 single: Controller; Managing errors
@@ -418,20 +442,6 @@ For example, imagine you're processing a :doc:`form </forms>` submission::
418442 return $this->render(...);
419443 }
420444
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-
435445After processing the request, the controller sets a flash message in the session
436446and then redirects. The message key (``notice `` in this example) can be anything:
437447you'll use this key to retrieve the message.
0 commit comments