You can support us by downloading this article as PDF from the Link below. Download the guide as PDF

Lately, I have been toying around with angularjs, and I have to confess, its quite a powerful framework. My side of the story.

Anyway, after reading much online here is my generic factory for consuming APIs(PHP SLIM 3).

[pastacode lang=”javascript” message=”” highlight=”” provider=”manual” manual=”app.factory(%22Data%22%2C%5B’%24resource’%2C’toaster’%2C%0Afunction(%24resource%2Ctoaster)%0A%7B%0Avar%20serviceBase%3D’http%3A%2F%2Flocalhost%2Fbookroom%2Fschool.php’%3B%2F%2Furl%0Areturn%20function(link)%20%2F%2Freceive%20part%20of%20url%20with%20parameters%0A%7B%0Areturn%20%24resource(serviceBase%2Blink%2C%7B%7D%2C%7B%0Aquery%3A%7Bmethod%3A’GET’%2CisArray%3Atrue%7D%2C%2F%2Fthis%20is%20for%20get%0Aupdate%3A%7Bmethod%3A’PUT’%7D%2C%0Adelete%3A%7Bmethod%3A’DELETE’%7D%2C%0Acreate%3A%7Bmethod%3A’POST’%7D%2C%0Atwo_query%3A%7B%0Aurl%3AserviceBase%2Blink%2C%0Amethod%3A’GET’%2CisArray%3Afalse%2C%0Aparams%3A%7Bid%3A’%40id’%2C%0Ausers_id%3A’%40users_id’%0A%7D%0A%7D%0A%0A%7D%0A%0A)%3B%0A%7D%0A%7D%5D)%3B%0A”/]

This is how it is used.
In your controllers

[pastacode lang=”javascript” message=”” highlight=”” provider=”manual” manual=”app.controller(‘usersCtrl’%2Cfunction(%24scope%2C%24rootScope%2C%24routeParams%2C%24location%2C%24http%2CData)%0A%7B%0A%C2%A0%C2%A0%C2%A0%20%24scope.login%3D%7B%7D%3B%0A%C2%A0%C2%A0%C2%A0%20%24scope.msgbox%3D%7B%7D%3B%0A%C2%A0%C2%A0%C2%A0%20%24scope.doLogin%3Dfunction(users)%0A%C2%A0%C2%A0%C2%A0%20%7B%0A%C2%A0%C2%A0%C2%A0%20%C2%A0%20Data(‘%2Flogin’).create(%7Busers%3Ausers%7D%2Cfunction(response)%7B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20if(response.message%3D%3D%22error%22)%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%2F%2FDo%20something%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7Delse%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%24location.path(‘dashboard’)%3B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7D%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7D).%24promise%3B%0A%C2%A0%C2%A0%20%7D%0A%C2%A0%C2%A0%C2%A0%20%24scope.doUserRegister%3Dfunction(user)%0A%C2%A0%C2%A0%C2%A0%20%7B%0A%C2%A0%C2%A0%C2%A0%20%C2%A0%C2%A0%C2%A0%20var%20user%3DData(‘%2Finput’).create(%7Bdata%3Auser%2Cid%3A1%7D%2Cfunction(response)%7B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%24scope.users%3Dresponse%3B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20console.log(%24scope.users)%3B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7D).%24promise%3B%0A%C2%A0%C2%A0%C2%A0%20%7D%7D)%3B%0Aapp.controller(‘userdisplayCtrl’%2Cfunction(%24scope%2C%24rootScope%2C%24routeParams%2C%24location%2C%24http%2CData)%0A%7B%0A%0A%C2%A0%C2%A0%C2%A0%20%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%24scope.users%3D%7B%7D%3B%0A%C2%A0%C2%A0%C2%A0%20%C2%A0%C2%A0%C2%A0%20Data(‘%2Fshow%2F%3Aid’).query(%7Bid%3A%201%7D%2Cfunction(response)%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%24scope.users%3Dresponse%3B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20console.log(%24scope.users)%3B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7D).%24promise%3B%0A%7D%7D)%3B%0A%0A%C2%A0%C2%A0%20%0A%2F%2FThis%20is%20while%20sending%20twoparameters%0A%0A%C2%A0Data(‘%2Fshow_2%2F%3Aid%2F%3Ausers_id’).two_query(%7Bid%3A%201%2Cusers_id%3A%24routeParams.users_id%7D%2Cfunction(response)%7B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%24scope.users%3Dresponse%3B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20console.log(response.email)%3B%0A%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%C2%A0%20%7D).%24promise%3B%0A%0A%7D)%3B”/]

I have not yet used PUT AND DELETE yet. Am quite uncomfortable with them

The rest api is like this, at the root of your site folder:

[pastacode lang=”javascript” message=”” highlight=”” provider=”manual” manual=”%24app-%3Epost(‘%2Finput’%2C%20function%20(%24req%2C%24res%2C%20%24args%20%3D%20%5B%5D)%20use(%24val%2C%24mesg%2C%24help%2C%24get_table)%0A%7B%0A%24id%20%3D%20%24req-%3EgetParsedBody()%5B’id’%5D%3B%20%2F%2Fchecks%20both%20_GET%20and%20_POST%20%5BNOT%20PSR-7%20Compliant%5D%0A%24data%20%3D%20%24req-%3EgetParsedBody()%5B’data’%5D%3B%0A%24table%3Dget_table(%24id)%3B%0A%2F%2Fdo%20something%0A%7D)%3B%0A%0A%24app-%3Eget(‘%2Fshow%2F%7Bid%7D’%2Cfunction(%24req%2C%24res%2C%24args)use(%24val%2C%24mesg%2C%24get_table)%0A%7B%0A%24id%3D%24req-%3EgetAttribute(‘id’)%3B%0A%0A%24table%3Dget_table(%24id)%3B%0A%2F%2FDo%20something%0A%7D)%3B%0A%24app-%3Eget(‘%2Fshow_2%2F%7Bid%7D%2F%7Busers_id%7D’%2Cfunction(%24req%2C%24res%2C%24args)use(%24val%2C%24mesg%2C%24get_table)%0A%7B%0A%24id%3D%24req-%3EgetAttribute(‘id’)%3B%0A%24users_id%3D%24req-%3EgetAttribute(‘users_id’)%3B%0A%24table%3Dget_table(%24id)%3B%0A%0A%2F%2Fdo%20something%0A%0A%7D)%3B”/]

The get_table() is a little function I built to allow me to select tables in my simple database

[pastacode lang=”javascript” message=”” highlight=”” provider=”manual” manual=”function%20get_table(%24id%3D%22%22)%0A%7B%0Aif(%24id%3D%3D1)%0Areturn%20%22admins%22%3B%0Aelseif(%24id%3D%3D2)%0Areturn%20%22students%22%3B%0Aelseif(%24id%3D%3D3)%0Areturn%20%22hostels%22%3B%0Aelseif(%24id%3D%3D4)%0Areturn%20%22lecturers%22%3B%0A%7D%0A%24get_table%3Dget_table()%3B”/]

If you do not understand, Please comment below and I will try help explain. Great week

You can support us by downloading this article as PDF from the Link below. Download the guide as PDF