8000 Adding details and usages of fetching the service as a controller arg · symfony/symfony-docs@45500b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 45500b3

Browse files
committed
Adding details and usages of fetching the service as a controller arg
1 parent 70178d1 commit 45500b3

File tree

5 files changed

+55
-28
lines changed

5 files changed

+55
-28
lines changed

controller.rst

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ controller classes. A great way to see the core functionality in
159159
action 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
247252
Accessing 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

255259
When extending the base ``Controller`` class, you can access any Symfony service
256260
via 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+
265272
What other services exist? To list all services, use the ``debug:container``
266273
console 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-
435445
After processing the request, the controller sets a flash message in the session
436446
and then redirects. The message key (``notice`` in this example) can be anything:
437447
you'll use this key to retrieve the message.

doctrine.rst

Lines changed: 7 additions & 0 deletions
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\Http 9E81 Foundation\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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 2 additions & 0 deletions
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)
0