What is the common API endpoint architecture in backends? [closed]

Hey I always used to create some pretty messy endpoints and I decided that it’s about time to start writing cleaner code, I’ll provide an example of how would I design an endpoint and I’ll be thankful for any insights why is it good or bad practice to do it like this also I’d be more than happy if you provide your own tweaks with explanation why would you do it like that.
Scenario: user post endpoint
Pathname: /api/user
Method: POST

class UserPostEndpoint extends AbstractController
    private UserPostHandlerInterface $handler;
    private SerializerInterface $serializer;

    function __construct(UserPostHandlerInterface $handler, SerializerInterface $serializer)
        $this->handler = $handler;
        $this->serializer = $serializer;

    public function handleRequest(Request $request): Response
        try {
            $userPostRequestDTO = $this->serializer->unserialize($request->getContent(), UserPostRequestDTO::class);
            $response = $this->handler->handle($userPostRequestDTO); // <- ApiSuccessResponse(statusCode = 201)
        } catch(SerializationException $e) {
            $response = new ApiErrorResponse('Invalid request content provided', 400);
        } catch(ApiRequestContentValidationException $e) {
            $response = new ApiErrorReponse($e->getPlainMessage(), 400);
        } catch(Exception $e) {
            $response = new ApiErrorReponse('Internal server error', 500);
        } finally {
            return $response;

Handler would contain some basic things like: invoke createUser in UserFactory, persist the entity with repository.
ApiErrorReponse: message, statusCode
ApiSuccessReponse: UserPostResponseDTO, statusCode(201)
Would you add / change / remove something from the given example to make it more "correct"?
Thanks for the insights

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *