function testMimeTypes() { $r = new Rest\Server(); $r->setAccept(array("*", "text/html", "application/json")); $r->addMap("GET", "/user", "Foo"); $r->addMap("GET", "/user/diogo", "Bar", array("application/json")); $this->assertEqual($r->getMap("GET", "/user", false), "Foo"); $this->assertEqual($r->getMap("GET", "/user.html", 'html'), "Foo"); $this->assertEqual($r->getMap("GET", "/user.json", 'json'), "Foo"); $this->assertEqual($r->getMap("GET", "/user.xml", 'xml'), "Foo"); $this->assertEqual($r->getMap("GET", "/user/diogo.json", 'json'), "Bar"); $this->assertEqual($r->getMap("GET", "/user/diogo.html", 'html'), "\\Rest\\Controller\\NotAcceptable"); }
// Now we query the database to check if supplied information is valid $auth = $db->prepare("select password from users where login = ?"); $auth->execute(array($login)); if (($a = $auth->fetchColumn(0)) != md5($salt . $pass)) { return false; } else { return true; } } // This a url mapping function, the core of the RestServer // You pass in a HTTP Method (like GET, POST, DELETE...) // A URL to map (this case is root "/") and a handler // The handler can be a closure receiving the server instance // Or your own Controller that implements RestController $server->addMap("GET", "/", function ($server) { // This class will redirect to index.html return new Rest\Controller\Redirect("index.html"); }, 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
{ $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; } } $q = isset($_GET["q"]) ? $_GET["q"] : ""; $r = new Rest\Server($q); $r->addMap("GET", "/Foo", "Foobar"); $r->addMap("POST", "/Foo", "Foobar"); $r->addMap("GET", "/Foo/bar", "Foobar::bar"); $r->addMap("GET", "/Foo/hello", "Foobar::foo"); $r->addMap("POST", "/Foo/hello", "Foobar::foo"); $r->addMap("GET", "/Foo/hello/[\\w]*", "Foobar::foo"); $r->addMap("GET", "/Foo/restricted/basic", "Foobar::auth"); $r->addMap("GET", "/Foo/restricted/digest", "Foobar::auth"); $r->addMap("GET", "/Foo/bench", "Foobar::bench"); $r->addMap("GET", "/Lambda", function ($rest) { $rest->getResponse()->setResponse("Hello Closure!"); return $rest; }); $r->addMap("GET", "/hello/:name", function ($rest) { $rest->getResponse()->setResponse("Hello, " . $rest->getRequest()->getParameter("name") . "!"); return $rest;