PDA

View Full Version : Simple PHP with direct...



TopKatz
17 Mar 2010, 3:45 AM
I recently had a use for a direct setup. It was a for a grocery list builder basicaly. Navigate some lists of items and add them to a master list of items. Then allow for edit, remove, and also a batch add. I figured this was a good use for direct, and I was right.

The problem was that I dont use a MVC or php frame work of any kind. All of my server side requests are handled in scripts that use a switch of sorts. In my request params I tell the server what Im trying to do, like:

{"routine":"getClientInfo","clientID":1234}

So as I looked into direct and how it worked with URI I was having a hard time figuring out how to deal with the writer API. Then I came up with the solution and figured I would share.

So directs API looks something like this:


api:{
read:'directRequest.php/readOrderItems',
create:'directRequest.php/newOrderItem',
update:'directRequest.php/updateOrderItem',
destroy:'directRequest.php/deleteOrderItem'
}


The trick is to get the server to look at the last part of the URI, the answer was to use the php $HTTP_SERVER_VARS['PATH_INFO'] variable. This holds that path info in it. So on the server I just:



$routine = $HTTP_SERVER_VARS['PATH_INFO'];

switch($routine) {

case('/deleteOrderItem'):
$recs = stripslashes($_POST['data']);
$recs = json_decode($recs);
$orderID = $_POST['orderID'];

foreach ($recs as $rec) {
mysql_query("DELETE FROM OrderItems
WHERE orderItemID = '$rec'
AND orderID = $orderID")or $err = mysql_error();
}

$response = '{"success":true,"message":"Record updated for the Order!"}';

print $response;
break;

}



Hope this helps

Katz

darren
18 Mar 2010, 12:51 PM
Interesting, thanks for sharing. Its really coll you can dictate the methods like that.