web services - Using a php file as webservice, but also as a normal include file -


We have written in webservice php . It's very simple, you implement the http request and webservice function in the same way with a certain "action" (post variable). This webservice is used by our application ( android and iOS )

We also have a user management system It has started in the form of creating / editing / deleting pages of users, but after that there is a lot to include more features than we have done through this php application Want to use some of the webservice functionality.

How it was created webservice :

  included "config.php"; // for database connection // Some additional classes and functions are included for $ action = $ _POST ["action"]; Switch ($ verb) {case "login": {// do login stuff break; } Case "get_roster": {// break roster inventory content; }}  

This file does not work when inserting the file from another php file, so I will call it function Put it in does not execute immediately). I added a $ userId variable to differentiate between a HT call and in one integration. If it is not set then the function is immediately called , if it is set, the function should be called from the file where webservice was included. Works, but it makes my eye lubricating to see this code. I really want a more beautiful solution, but I'm not sure it's possible. How it's coded now:

  Include "config.php"; // for database connection // for some additional classes and functions // There is no way to set $ user while making an HTTP request / you can set $ user when this file is included Firstly if startService () is not called immediately (! Isset ($ userId)) {startService (); } Function startService () {$ action = $ _POST ["verb"]; Switch ($ verb) {case "login": {// do login stuff break; } Case "get_roster": {// break roster inventory content; }}}  

What is the best way to achieve this behavior? What I need is that the switch case is executed immediately when http requests , but immediately when this file is in the other php file Is not included.

This is very trivial, in fact: the function declaration and function allocation differ in two different files Please. / P>

services.php

  function startService ($ action) {...}  

Go.php

  need_once 'services.php'; StartService ($ _post ['action']);  

If you insert the declaration and code orientation and additionally to inject arguments As shown above, you are making your code very flexible, reusable and maintenance. What do you have here that is essentially the beginning of the appropriate controller in the MVC terms.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -