Skip to content

Commit cedb1df

Browse files
committed
[DependencyInjection] Fixed public service use case
1 parent 8b95859 commit cedb1df

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

service_container.rst

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -815,24 +815,36 @@ loss, enable the compiler pass in your application.
815815
Public Versus Private Services
816816
------------------------------
817817

818-
Thanks to the ``_defaults`` section in ``services.yaml``, every service defined in
819-
this file is ``public: false`` by default.
818+
Starting with Symfony 4, services are private by default.
820819

821820
What does this mean? When a service **is** public, you can access it directly
822-
from the container object, which is accessible from any controller that extends
823-
``Controller``::
821+
from the container object, which can also be injected thanks to autowiring.
822+
This is mostly useful when you want to fetch services lazily::
824823

825-
use App\Service\MessageGenerator;
824+
namespace App\Service;
826825

827-
// ...
828-
public function new()
826+
class MessageGenerator
829827
{
830-
// there IS a public "logger" service in the container
831-
$logger = $this->container->get('logger');
828+
private $container;
832829

833-
// this will NOT work: MessageGenerator is a private service
834-
$generator = $this->container->get(MessageGenerator::class);
835-
}
830+
public function __construct(ContainerInterface $container)
831+
{
832+
$this->container = $container;
833+
}
834+
835+
public function generate(string $message, string $template = null, array $context = []): string
836+
{
837+
if ($template && $this->container->has('twig')) {
838+
// there IS a public "twig" service in the container
839+
$twig = $this->container->get('twig');
840+
841+
return $twig->render($template, $context + ['message' => $message]);
842+
}
843+
844+
// if no template is passed, the Twig service will not be loaded
845+
846+
// ...
847+
}
836848

837849
As a best practice, you should only create *private* services, which will happen
838850
automatically. And also, you should *not* use the ``$container->get()`` method to
@@ -849,7 +861,7 @@ But, if you *do* need to make a service public, override the ``public`` setting:
849861
# ... same code as before
850862
851863
# explicitly configure the service
852-
App\Service\MessageGenerator:
864+
App\SomePublicService:
853865
public: true
854866
855867
.. code-block:: xml
@@ -865,7 +877,7 @@ But, if you *do* need to make a service public, override the ``public`` setting:
865877
<!-- ... same code as before -->
866878
867879
<!-- Explicitly configure the service -->
868-
<service id="App\Service\MessageGenerator" public="true"></service>
880+
<service id="App\SomePublicService" public="true"></service>
869881
</services>
870882
</container>
871883
@@ -874,13 +886,13 @@ But, if you *do* need to make a service public, override the ``public`` setting:
874886
// config/services.php
875887
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
876888
877-
use App\Service\MessageGenerator;
889+
use App\SomePublicService;
878890
879891
return function(ContainerConfigurator $configurator) {
880892
// ... same as code before
881893
882894
// explicitly configure the service
883-
$services->set(MessageGenerator::class)
895+
$services->set(SomeService::class)
884896
->public()
885897
;
886898
};

0 commit comments

Comments
 (0)