My Laravel project has a lot of controller methods which simply return nothing or void
on success. I want (more precisely need) those methods to eventually return a 204 No Content
response to the client. I would like to avoid adding return response()->noContent()
to each and every controller method where I need it. However, if I do not, Laravel sends an 200 OK
response with an empty body to the client.
The method Illuminate\Routing\Router::toResponse($request, $response)
is responsible for this. The parameter $response
equals the return value of the controller method. If the controller method returns nothing, $response
equals null
. The method toResponse($request, $response)
matches the parameter $response
against different conditions and if nothing matches toResponse($request, $response)
creates a SymfonyResponse
with status code 200 and an empty body. I would like to override this method and add another condition. If $response === null
, then toResponse($request, $response)
should create a SymfonfyResponse
with status code 204.
My main problem is that I don't find any information how to make the framework to call my own implementation of the router class.