4

OK - let me begin by saying that this is going to come off as a hand-me-down type of question. I am an experienced CakePHP developer and a complete newbie at Angular.

I have a REST enabled backend written in CakePHP v3. I am now looking to create a front-end enabled by Angular for the same. The web app has about 25 controllers on the backend about 50 tables - so it is a fairly complicated app.

Questions

  1. I understand Angular doesnt really maintain state between pages or requests. And for a backend app to be truly 'RESTful' it should also not rely on state but authenticate every request / action - how do I do this? Once a user logs in - how do I create a token that is then exchanged between Angular and CakePHP for every request call?

  2. When the user moves between different pages on the front-end, how do I track the same and maintain authentication access in Angular?

Again - if these questions seem like hand-me-downs but examples and documentation is really scarce for integrating Angular with CakePHP. I am hoping that I can build some knowledge base around these eventually.

ChicagoSky
  • 1,290
  • 3
  • 22
  • 50

2 Answers2

2

Actually it is possible to use angular on apps that are not following the single page approach. You can use directives and controllers directly inside the HTML of every page.

Authentication is not an issue in this case as the server knows who you are based on the session, it should be present. This is actually what we do in a legacy app that might become a SPA in the far future.

If you need authentication between requests I would recommend to use JWT tokens. There is even a plugin for CakePHP that implements a proper auth adapter for JWT: https://github.com/ADmad/cakephp-jwt-auth I've even written an article about that topic: http://florian-kraemer.net/2014/07/cakephp-and-token-based-auth-with-angular-js/

floriank
  • 25,546
  • 9
  • 42
  • 66
0

I am not familiar with cake, but you seem to be not accounting for angular's approach to building apps. Angular expects a single page app. There is only a single page. So when you say "pages or requests", you really mean two different things. Angular will issue many requests to generate a single "page". From a user's point of view, it may look like many pages, but angular typically relies on a single index.html file.

As for authentication, again I am not sure what Cake is expecting, but typically, you just send your authentication token over as a cookie, or in your http header (depending on the kind of auth you are using). Angular will maintain its state for the entire time the page is open and will typically store the auth token appropriately.

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148
  • Let me give an example - in the traditional way of building apps with Cake, I have controllers based on certain usecases. For example, I have a CarController and then a DealerController and then a CustomerController. So if I am going to add, edit, delete, view Cars then I would have 4 different pages for each of these and all would be managed by the CarController. Likewise for Customer, Dealer etc. I understand that all 4 actions (add, edit, delete, view) would be one page in Angular, but if I want to move from Car to Customer I would technically need to move to a different page right? – ChicagoSky Sep 25 '15 at 05:22
  • 1
    Some [pratical example of Login with Angular+CakePHP](https://medium.com/@ranostaj/login-with-cakephp3-and-angularjs-b4b124708086) – Peter Krauss Feb 14 '16 at 11:45