Where to place the classes used by the service in Laravel?

A question, when using a service layer where to locate classes used by the service? For example: in the system there are orders, that the order was issued automatically it needs to meet certain requirements (rules). Suppose we have a service OrderService which is a method attemptAutoCheckout() (attempt automatic registration, the actual method that will check the order and if successful, to execute). But there appears not only a service, and another class OrderValidator who checks according to certain rules that implement the interface RuleInterface.

It turns out that in the controller we call


inside this method, we have:

$this->orderValidator->addRule(new FooRule());
$this->orderValidator->addRule(new BarRule());

if (! $this->orderValidator->validate($order)) {
 $errors = $this->orderValidator->getErrorMessages();

The question of where to place OrderValidator , RuleInterface , FooRule , BarRule ? If you just throw next to the OrderService in the directory Services/ it is not clear what belongs to what (and in fact OrderValidator as logically associated with the OrderService and beyond is unlikely to be used) in a separate directory to submit? Or what? What would you do? What structure is easier to ask?

In this embodiment, it turns tautology (App\Services\Order\OrderService)


If all fold to the root Services/ - it is not clear that all for what belongs.
If you create classes in the project root, it is also not clear to the validator why? Good it needs to be close to the service class.
April 7th 20 at 15:36
1 answer
April 7th 20 at 15:38
OrderService rename OrderUseCase (use) or Handler (handler) when using this architecture:
src/UseCase/Order/AttemptAutoCheckout/Handler.php //$handler->handle($command)
src/Service/Order/Validator/Validator.php //$validator->validate()
Damn, always strained namespaces of the type of Service\Order\Validator\Validator :D - alfonzo_Ber commented on April 7th 20 at 15:41
@Domenick.Hoeger41, it's just a grouping) If you don't like it can call a different Handler or something.

But in namespace you all will be pure and clear.

use Service\Order\Validator
new Validator();

What does it do? You have in the folder may be in addition to Validator.php and ValidatorInterface.php and a whole pile of rules (Rule) - Amani_Greenho commented on April 7th 20 at 15:44
@Maverick.Dietrich57, in this case, it falls off psr-4 autoloading, and have generates a classmap using the-o flag (ie, psr-4 autoloader looking for to match directories). But it's the lyrics, not super critical. - alfonzo_Ber commented on April 7th 20 at 15:47
@Domenick.Hoeger41, you, probably, something not correctly understood. I gave You the example of PSR 4. I do not propose to create two folders Validator. In my example Validator 1 folder in which the file Validator.php and one more folder Rules. Most likely, because of this confused. Folder Validator is used only for grouping. If we had only the file Validator, we would not have created the folder. - Amani_Greenho commented on April 7th 20 at 15:50
@Maverick.Dietrich57, and the interface validation rules where better to put on a level with the implementations (folder Rules) or above? - alfonzo_Ber commented on April 7th 20 at 15:53
@Domenick.Hoeger41one level with the rules. - Amani_Greenho commented on April 7th 20 at 15:56
@Maverick.Dietrich57, Thanks, wait, maybe more ideas will write later marked one of the solutions) - alfonzo_Ber commented on April 7th 20 at 15:59
Structure so so. Why Order separate from the rest? If you change the Order, it will change the rules and the validator, which means that following srp (that change together, should be together), Order must be in the same module (!= folder) with all the others. For example, like this:

- samir.Metz15 commented on April 7th 20 at 16:02
OrderService as OrderUseCases bad name. For the folder UseCases rules for the suffix - no!

Explains will to the contrary. OrderValidator validates, OrderSender sends OrderStatusChecker checks that makes a OrderUseCase? :) Right, that he pushed and the name is not reflecting! As Service Manager/Helper - carole.Wiegand commented on April 7th 20 at 16:05
And architecture is not unfolding in folders. - Monica53 commented on April 7th 20 at 16:08
For the folder UseCases rules for the suffix - no!
I agree with that) Just the thought drove it to the folder, so it happened. The name of OrderService, you will agree, is also a dubious name) Services is a common concept. - Amani_Greenho commented on April 7th 20 at 16:11
@Nestor.Marquardt73, Yes. Your little edit with the model will be correct. So how can the whole domain layer to combine models or DomainModel.

That's what I do at home. @user - Amani_Greenho commented on April 7th 20 at 16:14

Find more questions by tags Designing softwarePHP