Created
April 1, 2013 16:56
-
-
Save huntlyc/5286168 to your computer and use it in GitHub Desktop.
Project: Creating A RESTful PHP Web Service With Tonic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <IfModule mod_rewrite.c> | |
| RewriteEngine On | |
| RewriteCond %{REQUEST_URI} !dispatch\.php$ | |
| RewriteCond %{REQUEST_FILENAME} !-f | |
| RewriteRule ^api/([^/\.]+)/([^/\.]+)/?$ dispatch.php [L,QSA] | |
| </IfModule> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // load Tonic library and our snippet resource | |
| require_once 'lib/tonic.php'; | |
| require_once 'api/SnippetResource.php'; | |
| $request = new Request(array('baseUri' => '/api')); | |
| try { | |
| $resource = $request->loadResource(); | |
| $response = $resource->exec($request); | |
| } catch (ResponseException $e) { | |
| //If we've caught an exception, then eleviate stress by playing pacman. | |
| header("Location: /error.php"); die(); | |
| } | |
| $response->output(); | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Single Snippet Resource | |
| * @uri /snippet/:idhash | |
| */ | |
| class SnippetResource extends Resource { | |
| function get($request, $idhash){ | |
| $response = new Response($request); | |
| $response->code = Response::OK; | |
| $response->addHeader('content-type', 'text/plain'); | |
| //Open up a connection to our snippet store | |
| $con = new Mongo(); | |
| $db = $con->codesnippets; | |
| $collection = $db->snippets; | |
| $mID = new MongoId($idhash); | |
| $snippet = $collection->findOne(array("_id" => $mID)); | |
| $snippetArray = array(); | |
| if(! is_null($snippet)){ | |
| $mID = new MongoId($snippet["_id"]); | |
| $sID = $mID->{'$id'}; | |
| $snippetArray = array('id' => $sID, | |
| 'title' => $snippet["title"], | |
| 'description' => $snippet["description"], | |
| 'links' => $snippet['links'], | |
| 'files' => $snippet['files'], | |
| 'dateCreated' => $snippet["date_created"], | |
| 'lastModified' => $snippet["last_modified"], | |
| 'views' => $snippet["views"]); | |
| } | |
| //Set the response body to be our encoded array. | |
| $response->body = json_encode($snippetArray); | |
| return $response; | |
| } | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Single Snippet Resource | |
| * @uri /snippet/:idhash | |
| */ | |
| class SnippetResource extends Resource { | |
| function get($request, $idhash){ | |
| $response = new Response($request); | |
| $response->code = Response::OK; | |
| $response->addHeader('content-type', 'text/plain'); | |
| //Open up a connection to our snippet store | |
| $con = new Mongo(); | |
| $db = $con->codesnippets; | |
| $collection = $db->snippets; | |
| $mID = new MongoId($idhash); | |
| $snippet = $collection->findOne(array("_id" => $mID)); | |
| $snippetArray = array(); | |
| if(! is_null($snippet)){ | |
| $mID = new MongoId($snippet["_id"]); | |
| $sID = $mID->{'$id'}; | |
| $snippetArray = array('id' => $sID, | |
| 'title' => $snippet["title"], | |
| 'description' => $snippet["description"], | |
| 'links' => $snippet['links'], | |
| 'files' => $snippet['files'], | |
| 'dateCreated' => $snippet["date_created"], | |
| 'lastModified' => $snippet["last_modified"], | |
| 'views' => $snippet["views"]); | |
| } | |
| //Set the response body to be our encoded array. | |
| $response->body = json_encode($snippetArray); | |
| return $response; | |
| } | |
| function post($request, $idhash){ | |
| $response = new Response($request); | |
| $response->code = Response::OK; | |
| $response->addHeader('content-type', 'text/plain'); | |
| //Open up a connection to our snippet store | |
| $con = new Mongo(); | |
| $db = $con->codesnippets; | |
| $collection = $db->snippets; | |
| if(isset($request->data) && $request->data != ""){ | |
| $newSnippet = json_decode($request->data); | |
| $collection->insert($newSnippet); | |
| $snippet = $collection->findOne($newSnippet); | |
| $mID = new MongoId($snippet["_id"]); | |
| $sID = $mID->{'$id'}; | |
| $snippetArray = array('id' => $sID, | |
| 'title' => $snippet["title"], | |
| 'description' => $snippet["description"], | |
| 'links' => $snippet['links'], | |
| 'files' => $snippet['files'], | |
| 'dateCreated' => $snippet["date_created"], | |
| 'lastModified' => $snippet["last_modified"], | |
| 'views' => $snippet["views"]); | |
| }else{ | |
| $snippetArray = array("err" => "no data provided"); | |
| } | |
| $response->body = json_encode($snippetArray); | |
| return $response; | |
| } | |
| function put($request, $idhash){ | |
| $response = new Response($request); | |
| $response->code = Response::OK; | |
| $response->addHeader('content-type', 'text/plain'); | |
| //Open up a connection to our snippet store | |
| $con = new Mongo(); | |
| $db = $con->codesnippets; | |
| $collection = $db->snippets; | |
| $snippetArray = array(); | |
| if(isset($request->data) && $request->data != ""){ | |
| $updatedSnippet = json_decode($request->data); | |
| $mID = new MongoId($idhash); | |
| $collection->update(array("_id" => $mID), $updatedSnippet); | |
| $snippet = $collection->findOne($updatedSnippet); | |
| $mID = new MongoId($snippet["_id"]); | |
| $sID = $mID->{'$id'}; | |
| $snippetArray = array('id' => $sID, | |
| 'title' => $snippet["title"], | |
| 'description' => $snippet["description"], | |
| 'links' => $snippet['links'], | |
| 'files' => $snippet['files'], | |
| 'dateCreated' => $snippet["date_created"], | |
| 'lastModified' => $snippet["last_modified"], | |
| 'views' => $snippet["views"]); | |
| }else{ | |
| $snippetArray = array("err" => "no data provided"); | |
| } | |
| $response->body = json_encode($snippetArray); | |
| return $response; | |
| } | |
| function delete($request, $idhash){ | |
| $response = new Response($request); | |
| $response->code = Response::OK; | |
| $response->addHeader('content-type', 'text/plain'); | |
| //Open up a connection to our snippet store | |
| $con = new Mongo(); | |
| $db = $con->codesnippets; | |
| $collection = $db->snippets; | |
| $mID = new MongoId($idhash); | |
| $collection->remove(array("_id" => $mID)); | |
| $response->body = json_encode(array("id" => $idhash, "status" => "removed")); | |
| return $response; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment