Skip to content

Commit 049df7d

Browse files
committed
Adding details and usages of fetching the service as a controller arg
1 parent 105801c commit 049df7d

File tree

5 files changed

+56
-29
lines changed

5 files changed

+56
-29
lines changed

controller.rst

+35-25
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,11 @@ that's available to you with or without the use of the base
148148
action is to look in the
149149
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class.
150150

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+
151156
.. index::
152157
single: Controller; Redirecting
153158

@@ -236,12 +241,11 @@ The Symfony templating system and Twig are explained more in the
236241
Accessing other Services
237242
~~~~~~~~~~~~~~~~~~~~~~~~
238243

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.
243247

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
245249
via the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::get`
246250
method of the ``Controller`` class. Here are several common services you might
247251
need::
@@ -252,6 +256,9 @@ need::
252256

253257
$mailer = $this->get('mailer');
254258

259+
// you can also fetch parameters
260+
$someParameter = $this->getParameter('some_parameter');
261+
255262
What other services exist? To list all services, use the ``debug:container``
256263
console command:
257264

@@ -261,14 +268,31 @@ console command:
261268
262269
For more information, see the :doc:`/service_container` article.
263270

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::
265276

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!');
270286

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``.
272296

273297
.. index::
274298
single: Controller; Managing errors
@@ -407,20 +431,6 @@ For example, imagine you're processing a :doc:`form </forms>` submission::
407431
return $this->render(...);
408432
}
409433

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-
424434
After processing the request, the controller sets a flash message in the session
425435
and then redirects. The message key (``notice`` in this example) can be anything:
426436
you'll use this key to retrieve the message.

doctrine.rst

+7
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ a controller, this is pretty easy. Add the following method to the
548548
// ...
549549
use AppBundle\Entity\Product;
550550
use Symfony\Component\HttpFoundation\Response;
551+
use Doctrine\ORM\EntityManagerInterface;
551552

552553
// ...
553554
public function createAction()
@@ -568,6 +569,12 @@ a controller, this is pretty easy. Add the following method to the
568569
return new Response('Saved new product with id '.$product->getId());
569570
}
570571

572+
// you can also receive the $em as an argument
573+
public function editAction(EntityManagerInterface $em)
574+
{
575+
// ...
576+
}
577+
571578
.. note::
572579

573580
If you're following along with this example, you'll need to create a

email.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ The Swift Mailer library works by creating, configuring and then sending
100100
of the message and is accessible via the ``mailer`` service. Overall, sending
101101
an email is pretty straightforward::
102102

103-
public function indexAction($name)
103+
public function indexAction($name, \Swift_Mailer $mailer)
104104
{
105105
$message = \Swift_Message::newInstance()
106106
->setSubject('Hello Email')
@@ -125,7 +125,11 @@ an email is pretty straightforward::
125125
)
126126
*/
127127
;
128-
$this->get('mailer')->send($message);
128+
129+
$mailer->send($message);
130+
131+
// or, you can also fetch the mailer service in this way
132+
// $this->get('mailer')->send($message);
129133

130134
return $this->render(...);
131135
}

logging.rst

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ Logging a Message
1010
To log a message, fetch the ``logger`` service from the container in
1111
your controller::
1212

13-
public function indexAction()
13+
use Psr\Log\LoggerInterface;
14+
15+
public function indexAction(LoggerInterface $logger)
1416
{
15-
$logger = $this->get('logger');
17+
// alternative way of getting the logger
18+
// $logger = $this->get('logger');
19+
1620
$logger->info('I just got the logger');
1721
$logger->error('An error occurred');
1822

service_container.rst

+2
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ service whose id is ``monolog.logger.request``.
511511
the *service* whose id is ``monolog.logger.request``, and not just the *string*
512512
``monolog.logger.request``.
513513

514+
.. _services-autoconfigure:
515+
514516
The autoconfigure Option
515517
------------------------
516518

0 commit comments

Comments
 (0)