@@ -815,24 +815,36 @@ loss, enable the compiler pass in your application.
815
815
Public Versus Private Services
816
816
------------------------------
817
817
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.
820
819
821
820
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 ::
824
823
825
- use App\Service\MessageGenerator ;
824
+ namespace App\Service;
826
825
827
- // ...
828
- public function new()
826
+ class MessageGenerator
829
827
{
830
- // there IS a public "logger" service in the container
831
- $logger = $this->container->get('logger');
828
+ private $container;
832
829
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
+ }
836
848
837
849
As a best practice, you should only create *private * services, which will happen
838
850
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:
849
861
# ... same code as before
850
862
851
863
# explicitly configure the service
852
- App\Service\MessageGenerator :
864
+ App\SomePublicService :
853
865
public : true
854
866
855
867
.. code-block :: xml
@@ -865,7 +877,7 @@ But, if you *do* need to make a service public, override the ``public`` setting:
865
877
<!-- ... same code as before -->
866
878
867
879
<!-- Explicitly configure the service -->
868
- <service id =" App\Service\MessageGenerator " public =" true" ></service >
880
+ <service id =" App\SomePublicService " public =" true" ></service >
869
881
</services >
870
882
</container >
871
883
@@ -874,13 +886,13 @@ But, if you *do* need to make a service public, override the ``public`` setting:
874
886
// config/services.php
875
887
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
876
888
877
- use App\Service\MessageGenerator ;
889
+ use App\SomePublicService ;
878
890
879
891
return function(ContainerConfigurator $configurator) {
880
892
// ... same as code before
881
893
882
894
// explicitly configure the service
883
- $services->set(MessageGenerator ::class)
895
+ $services->set(SomePublicService ::class)
884
896
->public()
885
897
;
886
898
};
0 commit comments