@@ -81,21 +81,37 @@ Symfony Standard Edition:
81
81
<prototype namespace =" AppBundle\Controller\" resource =" ../../src/AppBundle/Controller" public =" true" >
82
82
<tag name =" controller.service_arguments" />
83
83
</prototype >
84
+
85
+ <!-- add more services, or override services that need manual wiring -->
84
86
</services >
85
87
</container >
86
88
87
89
.. code-block :: php
88
90
89
91
// app/config/services.php
92
+ use Symfony\Component\DependencyInjection\Definition;
90
93
91
- // _defaults and loading entire directories is not possible with PHP configuration
92
- // you need to define your services one-by-one
93
- use AppBundle\Controller\DefaultController;
94
+ // To use as default template
95
+ $definition = new Definition();
94
96
95
- $container->autowire(DefaultController::class)
97
+ $definition
98
+ ->setAutowired(true)
96
99
->setAutoconfigured(true)
100
+ ->setPublic(false)
101
+ ;
102
+
103
+ $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
104
+
105
+ // Changes default config
106
+ $definition
107
+ ->setPublic(true)
97
108
->addTag('controller.service_arguments')
98
- ->setPublic(true);
109
+ ;
110
+
111
+ // $this is a reference to the current loader
112
+ $this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
113
+
114
+ // add more services, or override services that need manual wiring
99
115
100
116
This small bit of configuration contains a paradigm shift of how services
101
117
are configured in Symfony.
@@ -147,9 +163,18 @@ thanks to the following config:
147
163
.. code-block :: php
148
164
149
165
// app/config/services.php
166
+ use Symfony\Component\DependencyInjection\Definition;
167
+
168
+ // To use as default template
169
+ $definition = new Definition();
150
170
151
- // services cannot be automatically loaded with PHP configuration
152
- // you need to define your services one-by-one
171
+ $definition
172
+ ->setAutowired(true)
173
+ ->setAutoconfigured(true)
174
+ ->setPublic(false)
175
+ ;
176
+
177
+ $this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
153
178
154
179
This means that every class in ``src/AppBundle/ `` is *available * to be used as a
155
180
service. And thanks to the ``_defaults `` section at the top of the file, all of
@@ -348,14 +373,12 @@ The third big change is that, in a new Symfony 3.3 project, your controllers are
348
373
349
374
// app/config/services.php
350
375
351
- // loading entire directories is not possible with PHP configuration
352
- // you need to define your services one-by-one
353
- use AppBundle\Controller\DefaultController;
376
+ // ...
377
+
378
+ // override default template
379
+ $definition->setPublic(true);
354
380
355
- $container->autowire(DefaultController::class)
356
- ->setAutoconfigured(true)
357
- ->addTag('controller.service_arguments')
358
- ->setPublic(true);
381
+ $this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
359
382
360
383
But, you might not even notice this. First, your controllers *can * still extend
361
384
the same base ``Controller `` class or a new :ref: `AbstractController <controller-abstract-versus-controller >`.
@@ -642,7 +665,7 @@ You're now ready to automatically register all services in ``src/AppBundle/``
642
665
+ AppBundle\:
643
666
+ resource: '../../src/AppBundle/*'
644
667
+ exclude: '../../src/AppBundle/{Entity,Repository}'
645
- +
668
+ +
646
669
+ AppBundle\Controller\:
647
670
+ resource: '../../src/AppBundle/Controller'
648
671
+ public: true
0 commit comments