@@ -815,20 +815,35 @@ Public Versus Private Services
815
815
From Symfony 4.0, every service defined is private by default.
816
816
817
817
What does this mean? When a service **is ** public, you can access it directly
818
- from the container object, which is accessible from any controller that extends
819
- `` Controller `` ::
818
+ from the container object, which can also be injected thanks to autowiring.
819
+ This is mostly useful when you want to fetch services lazily ::
820
820
821
- use App\Service\MessageGenerator ;
821
+ namespace App\Generator ;
822
822
823
- // ...
824
- public function new()
823
+ use Psr\Container\ContainerInterface;
824
+
825
+ class MessageGenerator
825
826
{
826
- // there IS a public "logger" service in the container
827
- $logger = $this->container->get('logger');
827
+ private $container;
828
828
829
- // this will NOT work: MessageGenerator is a private service
830
- $generator = $this->container->get(MessageGenerator::class);
831
- }
829
+ public function __construct(ContainerInterface $container)
830
+ {
831
+ $this->container = $container;
832
+ }
833
+
834
+ public function generate(string $message, string $template = null, array $context = []): string
835
+ {
836
+ if ($template && $this->container->has('twig')) {
837
+ // there IS a public "twig" service in the container
838
+ $twig = $this->container->get('twig');
839
+
840
+ return $twig->render($template, $context + ['message' => $message]);
841
+ }
842
+
843
+ // if no template is passed, the "twig" service will not be loaded
844
+
845
+ // ...
846
+ }
832
847
833
848
As a best practice, you should only create *private * services, which will happen
834
849
automatically. And also, you should *not * use the ``$container->get() `` method to
@@ -845,7 +860,7 @@ But, if you *do* need to make a service public, override the ``public`` setting:
845
860
# ... same code as before
846
861
847
862
# explicitly configure the service
848
- App\Service\MessageGenerator :
863
+ Acme\PublicService :
849
864
public : true
850
865
851
866
.. code-block :: xml
@@ -861,7 +876,7 @@ But, if you *do* need to make a service public, override the ``public`` setting:
861
876
<!-- ... same code as before -->
862
877
863
878
<!-- Explicitly configure the service -->
864
- <service id =" App\Service\MessageGenerator " public =" true" ></service >
879
+ <service id =" Acme\PublicService " public =" true" ></service >
865
880
</services >
866
881
</container >
867
882
@@ -870,13 +885,13 @@ But, if you *do* need to make a service public, override the ``public`` setting:
870
885
// config/services.php
871
886
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
872
887
873
- use App\Service\MessageGenerator ;
888
+ use Acme\PublicService ;
874
889
875
890
return function(ContainerConfigurator $configurator) {
876
891
// ... same as code before
877
892
878
893
// explicitly configure the service
879
- $services->set(MessageGenerator ::class)
894
+ $services->set(PublicService ::class)
880
895
->public()
881
896
;
882
897
};
0 commit comments