function testQueries() { $r = new Rest\Server(); $t = count(explode("/", $_SERVER["REQUEST_URI"])) - 2; $this->assertEqual($r->getQuery($t), "tests"); $this->assertEqual($r->getRequest()->getURI($t), "tests"); $r->setQuery("/teste/me"); $this->assertEqual($r->getQuery(2), "me"); $this->assertEqual($r->getRequest()->getURI(2), "me"); }
public function execute(Rest\Server $rest) { $r = "Method=" . $rest->getRequest()->getMethod() . "\n"; $r .= "URI=" . $rest->getRequest()->getRequestURI() . "\n"; foreach ($rest->getRequest()->getGET() as $key => $value) { $r .= "GET[" . $key . "]=" . $value . "\n"; } foreach ($rest->getRequest()->getPOST() as $key => $value) { $r .= "POST[" . $key . "]=" . $value . "\n"; } $rest->getResponse()->setResponse(nl2br($r)); return $rest; }
}, array("*", "text/html")); // The last parameter on the mapping can override server accepted mimes $server->addMap("GET", "/index", function ($server) { // For the root access we will present the user interface // For this we have a dummy view that just loads up a php file return new Rest\View\Generic("ui.php"); }, array("text/html")); // Here we map POST /users to user creation function $server->addMap("POST", "/users", function ($server) { // Again we recover set global items $db = $server->getParameter("db"); $salt = $server->getParameter("salt"); // The full request of the user, including headers and post/put data // Is available on the RestRequest object, accesible using // The getRequest() method of the server $user = json_decode($server->getRequest()->getBody()); // This is the raw body of the request // We them test if it's unique $unique = $db->prepare("select count(*) from users where login = ?"); $unique->execute(array($user->login)); // If not we foward the server to the BadRequest handler if ($unique->fetchColumn(0) != 0) { new \Rest\Controller\BadRequest(); } // Them we really insert it $insert = $db->prepare("insert into users (login, password, name) values (?,?,?)"); $insert->execute(array($user->login, md5($salt . $user->password), $user->name)); // The response object hold the data that you be sent back to the client // You can get it using the getResponse() method of the server instance $server->getResponse()->setResponse(json_encode($user)); // Them we send back the supplied user