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

Ranx99's avatar

Consume same API for both web and mobile or not?

Lets say I am building some large application ( multi-page app ) . And laravel will allow me to make an API and a website on the same application.

Since the website and the API will communicate with the same database, I was wondering if it is better to consume the same API for the website using Vue.js.

So this means I will make single entry point to the database for all the clients ( web , mobile..etc ).

And my plan is to make:

  • ApiControllers ( communicate with the database and return data )
  • WebControllers ( return blade views which have vue.js components to consume the API ) there is no communication with the database in these controllers.

What do you suggest?

0 likes
9 replies
Ranx99's avatar

@jlrdw I know the difference between the tow, I don't think you understand my question or I didn't make it clear?

audunru's avatar

I would do what you’re suggesting. You will typically have very few controllers for the blade views, as once the Vue app is loaded you will most likely use vue-router from there on. You will have the benefit of having fewer controllers to send and receive data, and your API tests will verify that the web and mobile clients can expect similar results as they’re both hitting the same endpoints.

Ranx99's avatar

@AUDUNRU - Thanks, but since you said to use vue-router .. this means I would go for a SPA? right? .. which I don't want to do...

Cronix's avatar

You don't have to use vue-router. You could do as you were suggesting.

However, I think it would be useful to pass the data to the view on initial pageload since you're doing it that way. Or at least page 1 if it's paginated. Then use Vue to maintain state and do crud operations (or whatever you're doing).

I would use the pure api approach if it were a SPA since you wouldn't be loading different views in subsequent requests. You could try both ways (preloading data vs ajax request) and benchmark them (should be quick/easy to do), but I think supplying data on initial page load will be a bit faster than issuing a separate request.

Ranx99's avatar

@CRONIX - @cronix thank you, so yeah its as you said :

preloading data vs ajax request..

Doesnt both are equally fast? since in preloading ( WebControllers ) I will communicate with database then call the view with the data and return it. ( VS ) In ( WebControllers ) only return a view then call API ( in vue ) to communicate with database for data?

If you had such a project ( not SPA ) which way you will go with?

Cronix's avatar

Doesnt both are equally fast?

I don't think so, but I'm just suggesting to benchmark both ways and decide which is really faster. It wouldn't take long to benchmark how long it takes sending all to the view, vs an additional ajax request after the view loads and the browser is ready to request it. Then you'd have numbers data to make your decision.

Ranx99's avatar

I don't care much about which one is faster that the other that much. but in ( preloading data vs ajax request ) context..

which one to choose? what issues should I think about to decide?

audunru's avatar

Again, I would do what you originally suggested. Use ApiControllers to comunicate with database. Use WebControllers to return blade views, then load data from ApiControllers in your Vue app. Fewer things that can break.

Please or to participate in this conversation.