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

Luís Nóbrega
4 min readJan 2, 2021
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?

--

--

Luís Nóbrega

Web Developer at 25friday. Likes to travel, write and being a father.