AngularJS and PHP backend

Cawa picture Cawa · Jul 29, 2013 · Viewed 19.2k times · Source

Maybe it's not a real question, rather is's a discussion. I decided to learn angular, using a simple task, build a blog system. And i have a few questions. Lest imagine that the php app will have the MVC structure, so i have some questions:

  • Should i build my back-end only as RESTFUL app, and use json response\request upon the angular and php?
  • What about the view in php app, i should use them with ng-init?
  • Routing, server side or client side?
  • What about caching?
  • And the last, but not the least, where i should put the logic about data that user will input?

Can someone give me the instructions or directions, about this things, and maybe useful link's to read the articles, to combine the php and angular, or maybe i'm doing it in the wrong way?

Answer

Petar Zivkovic picture Petar Zivkovic · Aug 2, 2013

You might want to consider this type of application as actually TWO applications.

The first is the backend, the API. You can use your PHP framework to build an API that will allow you to have data persistency, validation (business logic), etc... and forget about the front end for now, you are only building an API for the backend data.

The second part of the app is the AngularJS frontend. This includes all of the views and everything that the client sees. None of that is coming from the backend.

This allows you to use the backend API (the PHP bit) to act as the data store, with it's own validation for safety, while having the seamless user experience and basic client side validation from AngularJS.

Routing is AngularJS, as that is the actual frontend that the client is using.

Caching can be done (if needed) in the backend, your API.

Validation will happen in both the frontend and the backend, although they can be slightly different if need be.

Remember, you build the backend strictly as an API, without consideration for the frontend (as if there will be more than one app using it), so it will have it's own validation rules and logic.

Hope that helps.