Skip to content

Commit f2f5770

Browse files
committed
minor #9181 Documented how to get container parameters as a service (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #9181). Discussion ---------- Documented how to get container parameters as a service This fixes #8999. Commits ------- ce9388a Documented how to get container parameters as a service
2 parents 59756c8 + ce9388a commit f2f5770

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

service_container.rst

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ made. To do that, you create a new class::
296296
->addPart(
297297
'Someone just updated the site. We told them: '.$happyMessage
298298
);
299-
299+
300300
return $this->mailer->send($message) > 0;
301301
}
302302
}
@@ -317,7 +317,7 @@ you can type-hint the new ``SiteUpdateManager`` class and use it::
317317
if ($siteUpdateManager->notifyOfSiteUpdate()) {
318318
$this->addFlash('success', 'Notification mail was sent successfully.');
319319
}
320-
320+
321321
// ...
322322
}
323323

@@ -690,6 +690,42 @@ argument for *any* service defined in this file! You can bind arguments by name
690690
The ``bind`` config can be also be applied to specific services or when loading many
691691
services at once (i.e. :ref:`service-psr4-loader`).
692692

693+
Getting Container Parameters as a Service
694+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
695+
696+
.. versionadded:: 4.1
697+
The feature to get container parameters as a service was introduced in Symfony 4.1.
698+
699+
If some service or controller needs lots of container parameters, there's an
700+
easier alternative to binding all of them with the ``services._defaults.bind``
701+
option. Type-hint any of its constructor arguments with the
702+
:class:`Symfony\\Component\\DependencyInjection\\ParameterBag\\ParameterBagInterface`
703+
or the new :class:`Symfony\\Component\\DependencyInjection\\ParameterBag\\ContainerBagInterface`
704+
and the service will get all container parameters in a
705+
:class:`Symfony\\Component\\DependencyInjection\\ParameterBag\\ParameterBag` object::
706+
707+
// src/Service/MessageGenerator.php
708+
// ...
709+
710+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
711+
712+
class MessageGenerator
713+
{
714+
private $params;
715+
716+
public function __construct(ParameterBagInterface $params)
717+
{
718+
$this->params = $params;
719+
}
720+
721+
public function someMethod()
722+
{
723+
// get any param from $this->params, which stores all container parameters
724+
$sender = $this->params->get('mailer_sender');
725+
// ...
726+
}
727+
}
728+
693729
.. _services-autowire:
694730

695731
The autowire Option

0 commit comments

Comments
 (0)