public function testMicroClass() { $this->specify("MVC Micro doesn't work as expected", function () { $handler = new \RestHandler(); $app = new Micro(); $app->get("/api/site", [$handler, "find"]); $app->post("/api/site/save", [$handler, "save"]); $app->delete("/api/site/delete/1", [$handler, "delete"]); //Getting the url from _url using GET $_SERVER["REQUEST_METHOD"] = "GET"; $_GET["_url"] = "/api/site"; $app->handle(); expect($handler->getNumberAccess())->equals(1); expect($handler->getTrace())->equals(["find"]); //Getting the url from _url using POST $_SERVER["REQUEST_METHOD"] = "POST"; $_GET["_url"] = "/api/site/save"; $app->handle(); expect($handler->getNumberAccess())->equals(2); expect($handler->getTrace())->equals(["find", "save"]); //Passing directly a URI $_SERVER["REQUEST_METHOD"] = "DELETE"; $_GET["_url"] = null; $app->handle("/api/site/delete/1"); expect($handler->getNumberAccess())->equals(3); expect($handler->getTrace())->equals(["find", "save", "delete"]); }); }
foreach ($status->getMessage() as $message) { $errors[] = $message->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Deletes robots based on primary key $app->delete('/api/robots/{id:[0-9]+}', function ($id) use($app) { $phql = "DELETE FROM Robots WHERE id = :id:"; $status = $app->modelsManager->executeQuery($phql, array('id' => $id)); // Create a response $response = new Response(); if ($status->success() == true) { $response->setJsonContent(array('status' => 'OK')); } else { // Change the HTTP status $response->setStatusCode(409, "Conflict"); $errors = array(); foreach ($status->getMessages() as $message) { $errors[] = $message->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); $app->notFound(function () use($app) { $app->response->setStatusCode(404, "Not Found")->sendHeaders(); echo 'This is crazy, but this page was not found!'; }); $app->handle();
* @link https://docs.phalconphp.com/en/latest/reference/di.html */ $di = new FactoryDefault(); // 设置db $di->set('db', function () { return new PdoMysql(array("host" => "localhost", "username" => "root", "password" => "", "dbname" => "new-encounter")); }); $app = new Micro($di); $app->get('/', function () { echo "Singou Encounter Back End API Server"; }); $app->get('/token', function () { return router('User', 'login', func_get_args()); }); $app->delete('/token', function () { return router('User', 'logout', func_get_args()); }); $app->get('/lottery', function () { return router(''); }); $app->post('/lottery', function () { return router(''); }); $app->notFound(function () { return router('Base', 'error', array('0001', 404)); }); $app->handle(); } catch (Exception $e) { echo "Exception: ", $e->getMessage(); } function router($controller, $action, $parameters)
if ($source) { $source->setData($put->data); $source->setType($put->type); $source->setName($put->name); if ($source->save()) { $app->response->setStatusCode(200, 'OK'); $app->response->setJsonContent(array('status' => 'OK'))->send(); } } else { $app->response->setStatusCode(404, 'Not Found'); $app->response->setJsonContent(array('status' => 'Not Found'))->send(); } }); // Delete existing source $app->delete('/sources/{id}', function ($id) use($app) { $source = Tabs\Models\Sources::findById((string) $id); if ($source) { if ($source->delete()) { $app->response->setStatusCode(200, 'OK'); $app->response->setJsonContent(array('status' => 'OK'))->send(); } } else { $app->response->setStatusCode(404, 'Not Found'); $app->response->setJsonContent(array('status' => 'Not Found'))->send(); } }); $app->notFound(function () use($app) { $app->response->setStatusCode(404, "Not Found"); $app->response->setJsonContent(array('status' => 'Not Found'))->send(); }); $app->handle();
<?php use Phalcon\Mvc\Micro; $app = new Micro(); // Retrieves all robots $app->get('/api/robots', function () { //echo 111; }); // Searches for robots with $name in their name $app->get('/api/robots/search/{name}', function ($name) { }); // Retrieves robots based on primary key $app->get('/api/robots/{id:[0-9]+}', function ($id) { }); // Adds a new robot $app->post('/api/robots', function () { }); // Updates robots based on primary key $app->put('/api/robots/{id:[0-9]+}', function () { }); // Deletes robots based on primary key $app->delete('/api/robots/{id:[0-9]+}', function () { }); //$app->handle()->getContent();
$response->setContentType('application/json'); return $response; }); /** * DELETE /messages/$id/like */ $app->delete('/api/messages/{id:[0-9]+}/like', function ($msg_id) { global $logged_in, $user_id, $err_login; $response = new Response(); if (!$logged_in) { $response->setStatusCode(403, 'Authentication error'); $response->setContentType('application/json'); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array(is_null($err_login) ? "User not logged in" : $err_login))); return $response; } $new_value = like($user_id, $msg_id, -1); if ($new_value === false) { // error $response->setStatusCode(400, 'Error'); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array(mysql_error()))); } else { $response->setJsonContent(array('value' => intval($new_value))); } $response->setContentType('application/json'); return $response; }); /** * PUT /messages/$id/bookmark */ $app->put('/api/messages/{id:[0-9]+}/bookmark', function ($msg_id) { global $logged_in, $user_id, $err_login; $response = new Response();