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.
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.
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.
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?
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.
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.