예제 #1
0
 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;
 }
예제 #2
0
    // 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
    // Given a valid insertion we tell the server to respond with a proper 201 Created
    return new \Rest\Controller\Created();
});
// The follow mapping are pretty much the same logic, but with different handlers
// I will comment now only on important diferences
// So here is a feature, you can map parts of the url to named parameters
// In this case the sencond part of the url will be on the login parameter
// See more below
$server->addMap("GET", "/users/:login", function ($server) {
    $db = $server->getParameter("db");
    // Here, on the Request, you get the named parameter
    $user = $server->getRequest()->getParameter("login");
    $exists = $db->prepare("select count(*) from users where login = ?");
    $exists->execute(array($user));