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"); }
<?php // First we include the restserver lib //include '../restserver.phar'; include '../src/Rest/Server.php'; include 'Items.php'; // Them we instantiate the server // Using the rewrite rule on .htaccess file the request url is passed into $_GET['q'] // Therefore we pass this parameter to restserver // If app is running on root and .htacces working, them restserver can guess the url $server = new Rest\Server($_GET["q"]); // The server handles proper mime-types, * is for no extension $server->setAccept(array("*", "application/json")); // Using the "setParameter" we can have some globaly available vars // This var will be accessible to all requests handlers // Here I define a salt for the user authentication $server->setParameter("salt", "&dcalsd-09o"); // Since this is just a demo I don't want to get much into // Logic and stuff, so I'm creating a database on sqlite on // the fly just to run something $create = !file_Exists("data.db"); if ($create) { touch("data.db"); } $db = new PDO("sqlite:data.db"); if ($create) { $db->query(file_get_contents("schema.sql")); } // Again using the server to setup some global database $server->setParameter("db", $db); // This is a dummy authentication function receiving