I have experience with both Yii 2 and AngularJS, but separately. I have two questions:
Is it possible to use AngularJS in Yii 2's view? I am asking possible instead of feasible, because I think the problem may have arrived at routing.
Also, is it fair enough (for performance) to use Yii 2 and AngularJS together? (Both are MVC so for modular, manageable code.)
I searched for a long time, but I was unable to find any proper resource. What is the explanation!
When AngularJS starts your application, it parses and processes this new markup from the template using the compiler. The loaded, transformed and rendered DOM is then called the view. The first kind of new markup are the directives. They apply special behavior to attributes or elements in the HTML.
AngularJS starts automatically when the web page has loaded. The ng-app directive tells AngularJS that the <div> element is the "owner" of an AngularJS application. The ng-model directive binds the value of the input field to the application variable name.
AngularJS is a structural framework for dynamic web apps. It lets you use HTML as your template language and lets you extend HTML's syntax to express your application's components clearly and succinctly. AngularJS's data binding and dependency injection eliminate much of the code you would otherwise have to write.
YES, you can use AngularJS in Yii 2 views after implementing a different rooting approach. Here is a tutorial to start with.
But NO, I don't recommend doing so (while both Yii 2 and AngularJS are great frameworks with native support of REST). So the proper way is to use AngularJS to build your frontend and use Yii 2 just to provide a server API. Here is a good structure to do so:
Structure by @AlekseiAkireikin from this Stack Overflow post
project/
backend/ // Yii 2 application
web/ // Public visible backend folder
index.php // Entry point
config/
controllers/
models/
...
frontend/
app/ // Your AngularJS application here
css/ // Styles (.less or .css)
img/ // Images
lib/ // Third-party libraries such as jQuery or AngularJS
js/ // .js files (controllers, services, etc.)
partials/ // Templates (.html)
index.html
tests/ // AngularJS tests
node_modules/
...
The Yii RESTful API framework will provide a clean API which can communicate with your built-in AngularJS application or maybe a future mobile app or even providing resources and/or services to other websites or software. If you care about performance then go with both and use REST. A well-structured RESTful application is great to easily build a good caching system with a flexible strategy behind. You can even host your backend and database on a server (like Amazon EC2) providing only JSON (and/or XML) data for minimum bandwidth use, and having your frontend stored on an optimized CDN (like Amazon S3 or other CDN provider) with lower cost and faster answers.
Here are two examples implementing AngularJS and Yii 2 within REST: this and this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With