@@ -876,142 +876,10 @@ the framework.
876
876
the ``swiftmailer `` key invokes the service extension from the
877
877
``SwiftmailerBundle ``, which registers the ``mailer `` service.
878
878
879
- .. index ::
880
- single: Service Container; Advanced configuration
881
-
882
- Advanced Container Configuration
883
- --------------------------------
884
-
885
- As we've seen, defining services inside the container is easy, generally
886
- involving a ``service `` configuration key and a few parameters. However,
887
- the container has several other tools available that help to *tag * services
888
- for special functionality, create more complex services, and perform operations
889
- after the container is built.
890
-
891
- Marking Services as public / private
892
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
893
-
894
- When defining services, you'll usually want to be able to access these definitions
895
- within your application code. These services are called ``public ``. For example,
896
- the ``doctrine `` service registered with the container when using the DoctrineBundle
897
- is a public service as you can access it via::
898
-
899
- $doctrine = $container->get('doctrine');
900
-
901
- However, there are use-cases when you don't want a service to be public. This
902
- is common when a service is only defined because it could be used as an
903
- argument for another service.
904
-
905
- .. note ::
906
-
907
- If you use a private service as an argument to more than one other service,
908
- this will result in two different instances being used as the instantiation
909
- of the private service is done inline (e.g. ``new PrivateFooBar() ``).
910
-
911
- Simply said: A service will be private when you do not want to access it
912
- directly from your code.
913
-
914
- Here is an example:
915
-
916
- .. configuration-block ::
917
-
918
- .. code-block :: yaml
919
-
920
- services :
921
- foo :
922
- class : Acme\HelloBundle\Foo
923
- public : false
924
-
925
- .. code-block :: xml
926
-
927
- <service id =" foo" class =" Acme\HelloBundle\Foo" public =" false" />
928
-
929
- .. code-block :: php
930
-
931
- $definition = new Definition('Acme\HelloBundle\Foo');
932
- $definition->setPublic(false);
933
- $container->setDefinition('foo', $definition);
934
-
935
- Now that the service is private, you *cannot * call::
936
-
937
- $container->get('foo');
938
-
939
- However, if a service has been marked as private, you can still alias it (see
940
- below) to access this service (via the alias).
941
-
942
- .. note ::
943
-
944
- Services are by default public.
945
-
946
- Aliasing
947
- ~~~~~~~~
948
-
949
- When using core or third party bundles within your application, you may want
950
- to use shortcuts to access some services. You can do so by aliasing them and,
951
- furthermore, you can even alias non-public services.
952
-
953
- .. configuration-block ::
954
-
955
- .. code-block :: yaml
956
-
957
- services :
958
- foo :
959
- class : Acme\HelloBundle\Foo
960
- bar :
961
- alias : foo
962
-
963
- .. code-block :: xml
964
-
965
- <service id =" foo" class =" Acme\HelloBundle\Foo" />
966
-
967
- <service id =" bar" alias =" foo" />
968
-
969
- .. code-block :: php
970
-
971
- $definition = new Definition('Acme\HelloBundle\Foo');
972
- $container->setDefinition('foo', $definition);
973
-
974
- $containerBuilder->setAlias('bar', 'foo');
975
-
976
- This means that when using the container directly, you can access the ``foo ``
977
- service by asking for the ``bar `` service like this::
978
-
979
- $container->get('bar'); // Would return the foo service
980
-
981
- Requiring files
982
- ~~~~~~~~~~~~~~~
983
-
984
- There might be use cases when you need to include another file just before
985
- the service itself gets loaded. To do so, you can use the ``file `` directive.
986
-
987
- .. configuration-block ::
988
-
989
- .. code-block :: yaml
990
-
991
- services :
992
- foo :
993
- class : Acme\HelloBundle\Foo\Bar
994
- file : %kernel.root_dir%/src/path/to/file/foo.php
995
-
996
- .. code-block :: xml
997
-
998
- <service id =" foo" class =" Acme\HelloBundle\Foo\Bar" >
999
- <file >%kernel.root_dir%/src/path/to/file/foo.php</file >
1000
- </service >
1001
-
1002
- .. code-block :: php
1003
-
1004
- $definition = new Definition('Acme\HelloBundle\Foo\Bar');
1005
- $definition->setFile('%kernel.root_dir%/src/path/to/file/foo.php');
1006
- $container->setDefinition('foo', $definition);
1007
-
1008
- Notice that symfony will internally call the PHP function require_once
1009
- which means that your file will be included only once per request.
1010
-
1011
879
.. _book-service-container-tags :
1012
880
1013
881
Tags (``tags ``)
1014
- ~~~~~~~~~~~~~~~
882
+ ---------------
1015
883
1016
884
In the same way that a blog post on the Web might be tagged with things such
1017
885
as "Symfony" or "PHP", services configured in your container can also be
@@ -1079,5 +947,6 @@ Learn more
1079
947
* :doc: `/cookbook/controller/service `
1080
948
* :doc: `/cookbook/service_container/scopes `
1081
949
* :doc: `/cookbook/service_container/compiler_passes `
950
+ * :doc: `/components/dependency_injection/advanced `
1082
951
1083
952
.. _`service-oriented architecture` : http://wikipedia.org/wiki/Service-oriented_architecture
0 commit comments