@@ -10,48 +10,20 @@ your app that's not specific to the framework (e.g. routing and controllers).
10
10
Domain classes, Doctrine entities and regular PHP classes that are used as
11
11
services are good examples of business logic.
12
12
13
- For most projects, you should store everything inside the AppBundle .
13
+ For most projects, you should store everything inside the `` src/ `` directory .
14
14
Inside here, you can create whatever directories you want to organize things:
15
15
16
16
.. code-block :: text
17
17
18
18
symfony-project/
19
- ├─ app/
19
+ ├─ config/
20
+ ├─ public/
20
21
├─ src/
21
- │ └─ AppBundle/
22
- │ └─ Utils/
23
- │ └─ MyClass.php
22
+ │ └─ Utils/
23
+ │ └─ MyClass.php
24
24
├─ tests/
25
25
├─ var/
26
- ├─ vendor/
27
- └─ web/
28
-
29
- Storing Classes Outside of the Bundle?
30
- --------------------------------------
31
-
32
- But there's no technical reason for putting business logic inside of a bundle.
33
- If you like, you can create your own namespace inside the ``src/ `` directory
34
- and put things there:
35
-
36
- .. code-block :: text
37
-
38
- symfony-project/
39
- ├─ app/
40
- ├─ src/
41
- │ ├─ Acme/
42
- │ │ └─ Utils/
43
- │ │ └─ MyClass.php
44
- │ └─ AppBundle/
45
- ├─ tests/
46
- ├─ var/
47
- ├─ vendor/
48
- └─ web/
49
-
50
- .. tip ::
51
-
52
- The recommended approach of using the ``AppBundle/ `` directory is for
53
- simplicity. If you're advanced enough to know what needs to live in
54
- a bundle and what can live outside of one, then feel free to do that.
26
+ └─ vendor/
55
27
56
28
Services: Naming and Format
57
29
---------------------------
@@ -60,13 +32,13 @@ The blog application needs a utility that can transform a post title (e.g.
60
32
"Hello World") into a slug (e.g. "hello-world"). The slug will be used as
61
33
part of the post URL.
62
34
63
- Let's create a new ``Slugger `` class inside ``src/AppBundle/ Utils/ `` and
35
+ Let's create a new ``Slugger `` class inside ``src/Utils/ `` and
64
36
add the following ``slugify() `` method:
65
37
66
38
.. code-block :: php
67
39
68
- // src/AppBundle/ Utils/Slugger.php
69
- namespace AppBundle \Utils;
40
+ // src/Utils/Slugger.php
41
+ namespace App \Utils;
70
42
71
43
class Slugger
72
44
{
@@ -82,12 +54,12 @@ Next, define a new service for that class.
82
54
83
55
.. code-block :: yaml
84
56
85
- # app/ config/services.yml
57
+ # config/services.yaml
86
58
services :
87
59
# ...
88
60
89
61
# use the fully-qualified class name as the service id
90
- AppBundle \Utils\Slugger :
62
+ App \Utils\Slugger :
91
63
public : false
92
64
93
65
.. note ::
@@ -110,15 +82,15 @@ Now you can use the custom slugger in any controller class, such as the
110
82
111
83
.. code-block :: php
112
84
113
- use AppBundle \Utils\Slugger;
85
+ use App \Utils\Slugger;
114
86
115
87
public function createAction(Request $request, Slugger $slugger)
116
88
{
117
89
// ...
118
90
119
91
// you can also fetch a public service like this
120
92
// but fetching services in this way is not considered a best practice
121
- // $slugger = $this->get('app.slugger' );
93
+ // $slugger = $this->get(Slugger::class );
122
94
123
95
if ($form->isSubmitted() && $form->isValid()) {
124
96
$slug = $slugger->slugify($post->getTitle());
@@ -129,7 +101,7 @@ Now you can use the custom slugger in any controller class, such as the
129
101
}
130
102
131
103
Services can also be :ref: `public or private <container-public >`. If you use the
132
- :ref: `default services.yml configuration <service-container-services-load-example >`,
104
+ :ref: `default services.yaml configuration <service-container-services-load-example >`,
133
105
all services are private by default.
134
106
135
107
.. best-practice ::
@@ -163,11 +135,11 @@ the class namespace as a parameter:
163
135
164
136
.. code-block :: yaml
165
137
166
- # app/ config/services.yml
138
+ # config/services.yaml
167
139
168
140
# service definition with class namespace as parameter
169
141
parameters :
170
- slugger.class : AppBundle \Utils\Slugger
142
+ slugger.class : App \Utils\Slugger
171
143
172
144
services :
173
145
app.slugger :
@@ -205,16 +177,10 @@ The three entities defined by our sample blog application are a good example:
205
177
symfony-project/
206
178
├─ ...
207
179
└─ src/
208
- └─ AppBundle/
209
- └─ Entity/
210
- ├─ Comment.php
211
- ├─ Post.php
212
- └─ User.php
213
-
214
- .. tip ::
215
-
216
- If you're more advanced, you can of course store them under your own
217
- namespace in ``src/ ``.
180
+ └─ Entity/
181
+ ├─ Comment.php
182
+ ├─ Post.php
183
+ └─ User.php
218
184
219
185
Doctrine Mapping Information
220
186
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -233,7 +199,7 @@ looking for mapping information:
233
199
234
200
.. code-block :: php
235
201
236
- namespace AppBundle \Entity;
202
+ namespace App \Entity;
237
203
238
204
use Doctrine\ORM\Mapping as ORM;
239
205
use Doctrine\Common\Collections\ArrayCollection;
@@ -309,31 +275,17 @@ the following command to install the Doctrine fixtures bundle:
309
275
310
276
$ composer require "doctrine/doctrine-fixtures-bundle"
311
277
312
- Then, enable the bundle in `` AppKernel.php `` , but only for the ``dev `` and
278
+ Then, this bundle is enabled automatically , but only for the ``dev `` and
313
279
``test `` environments:
314
280
315
281
.. code-block :: php
316
282
317
- use Symfony\Component\HttpKernel\Kernel;
318
-
319
- class AppKernel extends Kernel
320
- {
321
- public function registerBundles()
322
- {
323
- $bundles = array(
324
- // ...
325
- );
326
-
327
- if (in_array($this->getEnvironment(), array('dev', 'test'))) {
328
- // ...
329
- $bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
330
- }
331
-
332
- return $bundles;
333
- }
283
+ // config/bundles.php
334
284
285
+ return [
335
286
// ...
336
- }
287
+ Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
288
+ ];
337
289
338
290
We recommend creating just *one * `fixture class `_ for simplicity, though
339
291
you're welcome to have more if that class gets quite large.
@@ -348,7 +300,7 @@ command:
348
300
349
301
Careful, database will be purged. Do you want to continue Y/N ? Y
350
302
> purging database
351
- > loading AppBundle \DataFixtures\ORM\LoadFixtures
303
+ > loading App \DataFixtures\ORM\LoadFixtures
352
304
353
305
Coding Standards
354
306
----------------
0 commit comments