AngularJS and Yii 2

(Dniznick) #1

Anyone using angular and Yii 2 together? I’m wondering if you could share your setup and philosophy. I have read about a few different approaches:

  • Using Yii as REST api backend only. Serve all frontend code from a static CDN, or even from the same frontend server but totally separate

  • Loading Angular as part of an asset bundle - one big single-page app (SPA)

I like the idea of still using Yii’s frontend code for the backend of my site (admin stuff) since it doesn’t really need to be fancy. Likewise, it seems like a lot of extra work and perhaps bad practice to use angular for the public-facing parts of my site since they consist mainly of information and semi-static content. As I understand, SEO is non-trivial with a SPA. It is really the logged-in, customer-facing portion that I think a SPA would enhance the experience. So I’m sort of leaning towards the second approach I have listed.

Where I get a little fuzzy on how it will work…

I’m thinking I will still need to build REST endpoints in Yii for the client based app. Or, is there a better way to accomplish this without a lot of code duplication? Can you provide an activerecord-based response and REST response from the same controller, using the same base model?

How would auth be handled? From what I read it sounds like best practice is to use some sort of token-based auth, OAuth, JWT, etc. What does Yii offer that would help with auth in this scenario?

Thanks to all who reply.

(Tvksv) #2

Somewhat tricky but not impossible. Most natural approach is to duplicate the logic in server-side templates (hey up, Ember’s handlebars), but for static pages it should be a piece of cake.

Had seen that before, wasn’t too fond of what it looked like. If you design your models and controllers while having REST in mind and keep the inheritance the right way, code duplication shouldn’t be a problem.

(Baldwindavidrye) #3

As I understand, SEO is non-trivial with a SPA.

No, that’s not true today. SPA SEO is not impossible now. You just need to know the technicalities behind it. You either need to develop the Single page site with server side rendering or pre rendering for it to be SEO friendly.