Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

leninpaulino's avatar

Remove stateful components (web routes, views, mvc-related stuff) from Laravel for building a REST API

Hi,

I'm about to start a new project using Laravel 7.x, a REST API, and the purpose of this project it's to remain always stateless.

I want to use Laravel for only building my API.

So, I'm wondering if it's common to delete stateful or views-related code (web routes, web middlewares, etc) for the sake of not having to maintain code that I will never use.

What do you do?

Do you just keep those files in the project and ignore them or you remove some?

0 likes
3 replies
rodrigo.pedra's avatar
Level 56

I keep them and ignore.

You might need them later, for example the view stuff is needed for sending HTML and Markdown based emails or notifications.

Later you might need to publish some api docs, so the web routes might come in hand for that.

Just use the api middleware stack and token guard and you will be fine most of the time.

The only thing you might want to tweak is exception handling, if the client making the request doesn't send the headers that Laravel needs to know they expect a JSON response it might send a redirect or render a HTML error page. Take a look into you exception handler (./app/Exceptions/Handler.php) and some of the auth related middleware to be sure you always return a JSON no matter if the request have all the headers Laravel needs to know the request expects a JSON response.

1 like

Please or to participate in this conversation.