Answer the question
In order to leave comments, you need to log in
Is it good practice to pass a Symfony\Component\HttpFoundation\Request to a service?
Code example:
class RealtyController extends Controller
{
public function searchAction(Request $request, RealtyService $realtyService)
{
$realties = $realtyService->getRealties($request);
return new JsonResponse($realties);
}
Answer the question
In order to leave comments, you need to log in
1. Work with request is used by high-level things like controllers, so you should not use them in your services. Your service is essentially a layer of communication between the application and your code. Therefore, it is best to transfer data from request to some kind of DTO, for example, from the CQRS pattern, this can be Command (), Query ()
2. Even when you need to transfer Request to some of your classes, it is better to use PSR interfaces for this. Then your Request will be framework independent. .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question