Skip to content

Commit b115943

Browse files
committed
Migrating "Business Logic" topics to Symfony Flex structure
1 parent a255be4 commit b115943

File tree

1 file changed

+27
-75
lines changed

1 file changed

+27
-75
lines changed

best_practices/business-logic.rst

Lines changed: 27 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,20 @@ your app that's not specific to the framework (e.g. routing and controllers).
1010
Domain classes, Doctrine entities and regular PHP classes that are used as
1111
services are good examples of business logic.
1212

13-
For most projects, you should store everything inside the AppBundle.
13+
For most projects, you should store everything inside the ``src/`` directory.
1414
Inside here, you can create whatever directories you want to organize things:
1515

1616
.. code-block:: text
1717
1818
symfony-project/
19-
├─ app/
19+
├─ config/
20+
├─ public/
2021
├─ src/
21-
│ └─ AppBundle/
22-
│ └─ Utils/
23-
│ └─ MyClass.php
22+
│ └─ Utils/
23+
│ └─ MyClass.php
2424
├─ tests/
2525
├─ 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/
5527
5628
Services: Naming and Format
5729
---------------------------
@@ -60,13 +32,13 @@ The blog application needs a utility that can transform a post title (e.g.
6032
"Hello World") into a slug (e.g. "hello-world"). The slug will be used as
6133
part of the post URL.
6234

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
6436
add the following ``slugify()`` method:
6537

6638
.. code-block:: php
6739
68-
// src/AppBundle/Utils/Slugger.php
69-
namespace AppBundle\Utils;
40+
// src/Utils/Slugger.php
41+
namespace App\Utils;
7042
7143
class Slugger
7244
{
@@ -82,12 +54,12 @@ Next, define a new service for that class.
8254

8355
.. code-block:: yaml
8456
85-
# app/config/services.yml
57+
# config/services.yaml
8658
services:
8759
# ...
8860
8961
# use the fully-qualified class name as the service id
90-
AppBundle\Utils\Slugger:
62+
App\Utils\Slugger:
9163
public: false
9264
9365
.. note::
@@ -110,15 +82,15 @@ Now you can use the custom slugger in any controller class, such as the
11082

11183
.. code-block:: php
11284
113-
use AppBundle\Utils\Slugger;
85+
use App\Utils\Slugger;
11486
11587
public function createAction(Request $request, Slugger $slugger)
11688
{
11789
// ...
11890
11991
// you can also fetch a public service like this
12092
// but fetching services in this way is not considered a best practice
121-
// $slugger = $this->get('app.slugger');
93+
// $slugger = $this->get(Slugger::class);
12294
12395
if ($form->isSubmitted() && $form->isValid()) {
12496
$slug = $slugger->slugify($post->getTitle());
@@ -129,7 +101,7 @@ Now you can use the custom slugger in any controller class, such as the
129101
}
130102
131103
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>`,
133105
all services are private by default.
134106

135107
.. best-practice::
@@ -163,11 +135,11 @@ the class namespace as a parameter:
163135

164136
.. code-block:: yaml
165137
166-
# app/config/services.yml
138+
# config/services.yaml
167139
168140
# service definition with class namespace as parameter
169141
parameters:
170-
slugger.class: AppBundle\Utils\Slugger
142+
slugger.class: App\Utils\Slugger
171143
172144
services:
173145
app.slugger:
@@ -205,16 +177,10 @@ The three entities defined by our sample blog application are a good example:
205177
symfony-project/
206178
├─ ...
207179
└─ 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
218184
219185
Doctrine Mapping Information
220186
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -233,7 +199,7 @@ looking for mapping information:
233199

234200
.. code-block:: php
235201
236-
namespace AppBundle\Entity;
202+
namespace App\Entity;
237203
238204
use Doctrine\ORM\Mapping as ORM;
239205
use Doctrine\Common\Collections\ArrayCollection;
@@ -309,31 +275,17 @@ the following command to install the Doctrine fixtures bundle:
309275
310276
$ composer require "doctrine/doctrine-fixtures-bundle"
311277
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
313279
``test`` environments:
314280

315281
.. code-block:: php
316282
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
334284
285+
return [
335286
// ...
336-
}
287+
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
288+
];
337289
338290
We recommend creating just *one* `fixture class`_ for simplicity, though
339291
you're welcome to have more if that class gets quite large.
@@ -348,7 +300,7 @@ command:
348300
349301
Careful, database will be purged. Do you want to continue Y/N ? Y
350302
> purging database
351-
> loading AppBundle\DataFixtures\ORM\LoadFixtures
303+
> loading App\DataFixtures\ORM\LoadFixtures
352304
353305
Coding Standards
354306
----------------

0 commit comments

Comments
 (0)