Yii2 Case Study (1) : Angular JS

This is an example how to combine Yii2 with AngularJS. We have one table, game.

1. Table Preparation

Table game :

Yii2 Case Study AngularJS

Create model Game.php using Gii, put it in common\models folder.

2. Create GameController.php, index.php, app.js and Use AngularJS 1.6

create frontend/controllers/GameController.php

create frontend/views/game/index.php

Use AngularJS 1.6, and put https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.min.js inside this file.

create frontend/web/app.js

If the result of {{6+4}} has become 10, it means the AngularJS has already running:

yii2 case study angularjs

3. Create the List

modify frontend/view/game/index.php

Yii2 AngularJS

I put a record through mySql to test the data display.

Modify app.js by adding gameList function:

Modify GameController.php by adding public function actionGameList() :

Yii2 Case Study AngularJS

4. Add a Record

Modify GameController.php, add public function actionCreate():

Modify app.js, add

Add one record and click Save, data will automatically stored in the database.

Yii2 AngularJS

5. Update Record

Modify GameController.php, add public function actionUpdate.

Modify app.js, add

Now try to update the Last of Us data by clicking ‘Update’ button, and change the value whatever you want:

Yii2 AngularJS

Click Save, now the record has changed.

6. Delete Record

Modify GameController.php, add public function actionDelete.

Modify app.js, add :

Now try to Delete a record.

So now the files will look like this:

GameController.php

index.php

app.js

Good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.