Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony bundle directory structure - where to put service classes

Tags:

php

symfony

In my Symfony 3 app I have several classes that do some kind of task, for example calculating amounts, finding things, naming directories...

Currently, they are all in Service directory, so directory structure looks something like this:

- AppBundle
 -- Controller
 -- Entity
 -- Form
 -- Service
   -- LiveStatsCalculator.php
   -- DirectoryNamer.php
   -- AdvertisementPlayCalendarFactory.php
   -- MediaConfigGenerator.php
   ...

Is this good approach, or should I separate directories, based on a task class is responsible for? For example:

- AppBundle
 -- Controller
 -- Calculator
   -- LiveStatsCalculator.php
 -- Entity
 -- Factory
   -- AdvertisementPlayCalendarFactory.php
 -- Form
 -- Namer
   -- DirectoryNamer.php

Or, perhaps, there is even better solution? :)

like image 567
Matko Đipalo Avatar asked Oct 24 '25 19:10

Matko Đipalo


1 Answers

The easiest way to learn best practices of framework is to look how the internal framework bundles are organized.

I.e you can go to github for symfony/framework-bundle https://github.com/symfony/framework-bundle

They have responsibility based naming solution and it is reasonable. Having all services stored in a single namespace would end in converting your service into trash can. Or you end in splitting namespaces inside your Services namepsace. I think it is not you want to end with.

Meaningful names and locaiton are always preferred.

like image 138
ScayTrase Avatar answered Oct 26 '25 10:10

ScayTrase