Laravel: How to serve requests with HTML and JSON (with DRY)

Photo by Kyle Sung on Unsplash

One of the common problems of a web application is the need to return HTML and JSON to a request. Why? Imagine we have a SPA who needs the responses in JSON, and a backend that needs HTML. In this article, I will show you some ways on how to accomplish this in Laravel.

Why is this a problem?