Udders Udders - 1 month ago 6
PHP Question

Laravel and Angluar

I am trying to teach myself angular while working on a side project. I am trying to ascertain a couple of things as I get going.

1) What the best way to setup such a project, do I a) have a single domain and consume an API from with a laravel project, or b) have website.com and api.website.com and consume the API as if it were a 3rd party API.

I can see pro's and cons for each, but what I can't get my head around is if I with A, how does routing work? I assume the initial routing would be via laravel to display a top level view and then from that point on wards Angular would do the routing, but surely angular and it's route only get initiated at page load, so if a user goes to say /about without hitting the site root, no route on laravel side exists for that and thus it would 404?

What is the best setup for consuming my own api with angular?

Answer

I suggest to separate (your option b).

  • Front and back-end are totally separated
  • You can replace one of them without problems in the other
  • Use middleware, json responses and http status codes
  • Use a framework for back-end too (for example Laravel or Lumen)

About routes ...

Your back-end has its own routes (endpoints).

Your front-end has its own routes (totally different), but should send GET/POST/PUT etc. requests to the back-end. The back-end returns (json) response, which will be parsed by the front-end.

Develop both separately! So you can use the back-end for third party later.

Comments