G
G
gitdev2021-05-27 00:21:12
symfony
gitdev, 2021-05-27 00:21:12

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);
    }


And the second: is it possible to pass a Request to the "Service layer" at all, because the code may become untestable.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2021-05-27
@gitdev

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 question

Ask a Question

731 491 924 answers to any question