Answer the question
In order to leave comments, you need to log in
What code should be moved to separate classes?
Hey!
I can't figure out at what point repetitive code should be moved to separate classes (let's call them handler(s)) from controllers.
Answer the question
In order to leave comments, you need to log in
You don't have to call them handlers.
Taking the example $result = ['my_data' => $var];
That is not necessary of course. But I understand what you mean. Although if you operate with objects to the fullest, then you can create something like responseMyData
The controller receives the request and gives the response by calling the handler. There shouldn't be a line of code in it anymore.
With the exception of some individual implementations. Here is an example of my controllers. removed under
<?php
class orderController extends AbstractController {
/**
* @ApiDoc(
* section="travelSystem",
* input=OrderRequest::class,
* statusCodes={
* 200="sucess",
* 404="Order not found"
* },
* views={"night_build"}
* )
* @RestDoc(
* security={Access::ORDER}
* )
*
* @return Response
*/
public function orderAction(OrderRequest $orderRequest): Response
{
// Команда - простой объект с геттерами
$command = new orderCommand(
$orderRequest->getHotel(),
$orderRequest->getCheckIn()
);
// Хендлер - в котором как раз и происходят все
// нужные нам операции по обработке
$this->get('commandBus')->handle($command);
return $this->responseForRest();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question