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

NickCourage's avatar

Creating and consuming an API service within Laravel

I would like to make an application I'm working on API driven and I understand that I could create this as a RESTful API service and then create a separate SPA to consume it. But what if I wanted to make this in a monolithic way where I used a mixture of Vue components, HTML, and blade templates for the views, etc within a single Laravel application?

Rather than follow what I guess is the traditional way i.e. create routes in web.php, reference the endpoints of those routes to a method in a respective controller, etc but what I'm trying to get my head around is how I could do this with the api.php routes and endpoints where I would just be serving responses that would contain JSON data, etc and how could I then implement these to use views as well?

Would I use api.php routes for the JSON response data and web.php routes for defining what views are returned? If so, how? Or, as this is all meant to be contained within a single Laravel application, just stick to using web.php?

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

But what if I wanted to make this in a monolithic way where I used a mixture of Vue components, HTML, and blade templates for the views, etc within a single Laravel application?

Take a look into Inertia.js

https://inertiajs.com/

It is meant for that approach. Seems a free-series on inertia will be coming in the near future:

https://twitter.com/laracasts/status/1310634863435931648

Or, as this is all meant to be contained within a single Laravel application, just stick to using web.php?

If is is all meant to be contained within a single Laravel application... Just stick to using web.php

From your description Inertia seems a good fit (Vue frontend + Laravel routes and traditional forms workflow).

But I'd also take a look into Livewire.

If it is all the same app, keep it simple.

Hope it helps.

1 like
NickCourage's avatar

@rodrigo.pedra Thank you Rodrigo - Inertia seems to fit alot of what I've described there so will be investigating it in detail. Thanks for the reply

1 like
rodrigo.pedra's avatar

You're welcome.

Let me share a secret:

If it is all the same app, keep it simple.

I learned this the hard way.

Wrote some (maybe many) apps with SPA/API separation for no reason other than "let's do what everybody is doing these days".

Routing is harder, auth is harder, storage is harder (at least in my mileage). If you don't need to provide a full API to all your app's feature to third-party (or first-party mobile apps), I don't recommend this approach.

Some of those apps are still today much more complicated to maintain/add features than traditional ones due to this architecture choice even when there were no other consumers.

But, as everything in software engineering, it always depends. If you have a dedicated frontend team with strong Nuxt/Next/Vue CLI skills, and your backend team will only look after the API, go for it. But in most of the projects I work on I am the solo developer, doing everything by yourself and dealing with all the caveats is not worth the time, specially when providing a full API is not a business requirement.

Of course after writing some SPAs you start liking some niceties from using Vue/React for frontend stuff. That is where Inertia comes handy. I was an early adopter and wrote several apps with it. No regrets yet.

Note I said "wrote several apps with it" not "for all new apps I wrote after learning Inertia, this is the only stack I go for".

For simpler apps, traditional request/response workflow with minimal JavaScript shines even more.

I also used Livewire recently for the first time, and have been amused by how much value it brings. I am still strong in the Inertia/Vue camp, but already see a lot of value using Livewire. It is surely worth your time learning about it.

At the end of the day, choose whatever stack you are more productive with, and brings more value to your client/users.

If you read until here, thanks for your patience. Hope something here can add some value on your future choices.

Have a nice day =)

1 like
NickCourage's avatar

I appreciate the info and insight - thank you. Inertia certainly seems to deal with the upside of why we would use an SPA/API driven microservice approach without the overhead and is quite blatant about the fact that it is monolithic - I like that. The demo app "PingCRM" really shows it off nicely and is exactly the kind of thing I'm interested in.

For a while, I got stuck into a way of thinking that put me off of building anything that wasn't API driven or didn't use a microservice architecture and quickly realized how thinly I would be spread as a soloist developer for my development endeavours.

Unless we're talking about building a unique platform like Twitter, Stackoverflow, etc that needs to scale vertically instead of horizontally, where like you say, there are those dedicated teams and abundant resources for individual aspects of development I realized it added a lot more depth to the things I just didn't (and still don't fully) know i.e. the added complexities of authorization, storage, AWS/Azure integration, etc.

I'll checkout Livewire and noticed that this and Intertia ships with Laravel 8 - something I haven't yet really exposed myself to as I'm still working with Laravel 6. The reason for this is that it's the most up to date LTS release and where I work, unless a 3rd party framework has an LTS version, we won't use it in a production environment and thought that was a good quality standard to follow for myself.

All the best Rodrigo

1 like
rodrigo.pedra's avatar

Thanks!

Share similar views as yours, about scaling and having dedicated teams to deal with complex moving parts.

All the best too, Nick.

martinbean's avatar

@nickcourage First I’d determine whether building API-first and building an SPA is actually going to bring you any benefits or advantages. If you decide it is, then you just build your API by putting your routes in your routes/api.php file, and then have a web controller that returns a Blade view, which in turn has a <script> tag that loads your Vue SPA’s entry point, i.e. /js/app.js.

2 likes

Please or to participate in this conversation.