Answer the question
In order to leave comments, you need to log in
How to organize logic in Symfony using inheritance?
Good afternoon
Please tell me the correct organization of the code in the symphony.
The situation, conditionally, is this: the Order entity (order) was originally created, and several actions in the OrderController for creating, searching and editing an order. Accordingly, each action had its own forms (CreateOrderForm, etc.) and views (create_order.html.twig, etc.). An OrderModel was also created, in which all the logic of processing and saving to the database is implemented.
Now, if an order has a certain attribute, it is necessary to implement logic that is partially different from the original one (additional fields appear for information about the order + some differences in processing).
For forms, you can create separate CreateNewOrderForm, etc., which you can inherit from existing ones. Views break up individual templates, transfer them to files, and draw the corresponding ones for different order types. To organize the logic, there is also an option to create a NewOrderModel, which you inherit from the existing one and override the necessary methods.
But then it turns out that the logic of the controller actions is the same, only the created form, the called model and the rendered view differ. Keep several identical controllers - duplication. write something like
if ($orderType == 'new') {
$form = $this->createForm(new CreateNewOrderForm());
} else {
$form = $this->createForm(new CreateOrderForm());
}
Answer the question
In order to leave comments, you need to log in
What are the better options?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question