24

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?

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
Cawa
  • 1,269
  • 3
  • 25
  • 44

2 Answers2

32

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.

Petar Zivkovic
  • 970
  • 10
  • 20
  • So you advice to use php only as api? And why its a bad idea mix php views with angular? – Cawa Aug 06 '13 at 12:50
  • 3
    @СашаХарьков - Well, I'm not sure if I'd say it's a bad idea per se, but conceptually it could introduce a lot more confusion and maintenance problems than it would initially solve. The idea is that AngularJS will provide you with the entire front end, it is an application in it's own right. The backend API (in this case PHP) is there to help persist data with CRUD operations. Separating them helps to keep code maintainable and scalable. – Petar Zivkovic Aug 07 '13 at 13:22
  • 1
    any particular PHP framework recommended for building this backend API? – domi Oct 08 '13 at 10:42
  • @domi - PHP frameworks are a subject with a million opinions and no right or wrong answers. It's really a matter of preference and the nature of the application. Choose the one that fits best with your needs and preferences. If I had to choose, I'd go for one with good XML and JSON support on account that the data returned from the API will likely be in that format. Hope that helps a bit. – Petar Zivkovic Oct 17 '13 at 21:11
  • indeed, but aren't most PHP framework all about mvc? I did a PHP backend API with codeigniter, but honestly I saw no real benefit compared to using no PHP framework at all. – domi Oct 19 '13 at 13:40
  • 1
    @domi - It's very possible (and some people prefer) to build the backend without use of a framework. You can just use a package manager like composer and grab the libraries you need, piece them together and viola. I personally like frameworks, makes things easy, and I'm lazy enough to really appreciate that. On a side note, I'd suggest a more modern framework with support for RESTful routes, CodeIgniter (while being my favorite) is a little dated. – Petar Zivkovic Oct 21 '13 at 19:04
6

I have found a very simple structure that allows me to utilize Angular with PHP and restful api's. I use Angularjs for all views. I use a restful PHP API framework called slim to facilitate the communications between Angular and the PHP models which I use Doctorine2 for.

85% of my coding is done with Angular(Views). 5% done with the API(controller) and the remaining 10% configuring business logic in the Models. Great separation of concerns and not much overhead. Simple and concise.

Jessica
  • 141
  • 3
  • 6