public function formAction() { $app = new \Phalcon\Mvc\Micro(); echo $app->request->getRawBody(); $app->post('/api/robots', function () use($app) { $robot = json_decode($app->request->getRawBody()); print_r($robot); }); }
$app->post('/balances', function () use($app, &$mysqli) { global $authinfo; //Create a response $response = new Phalcon\Http\Response(); $payload = $app->request->getRawBody(); $payload = json_decode($payload, true); if (!array_key_exists('CONTENT_LENGTH', $_SERVER) || intval($_SERVER['CONTENT_LENGTH']) == 0 || !is_array($payload) || count($payload) == 0) { //Change the HTTP status $response->setStatusCode(400, "Bad Request"); //Send errors to the client $response->setJsonContent(array('status' => 'ERROR', 'messages' => array('Payload is wrong or CONTENT_LENGTH is missing'))); } else { $sqlbal = array(); foreach ($payload as $node) { $sqlbal[] = sprintf("(%d,'%s',%.9f,'%s')", $node['TestNet'], $mysqli->real_escape_string($node['PubKey']), $node['Balance'], $mysqli->real_escape_string($node['LastUpdate'])); } $sql = "INSERT INTO cmd_info_masternode_balance (TestNet, PubKey, Balance, LastUpdate)" . " VALUES " . implode(',', $sqlbal) . " ON DUPLICATE KEY UPDATE Balance = VALUES(Balance), LastUpdate = VALUES(LastUpdate)"; if ($result = $mysqli->query($sql)) { $info = $mysqli->info; if (is_null($info)) { $info = true; } //Change the HTTP status $response->setStatusCode(202, "Accepted"); $response->setJsonContent(array('status' => 'OK', 'data' => array('balances' => $info))); } else { $response->setStatusCode(503, "Service Unavailable"); $response->setJsonContent(array('status' => 'ERROR', 'messages' => array($mysqli->errno . ': ' . $mysqli->error))); } } return $response; });
} $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Send message to a selected user. $app->post('/chat/{sender}/{receiver}', function ($sender, $receiver) use($app) { $message = $app->request->getJsonRawBody(); $phql = "INSERT INTO Messages (sender_id, receiver_id, message_content)\n \t\tVALUES (:sender_id:, :receiver_id:, :message_content:)"; $status = $app->modelsManager->executeQuery($phql, array('sender_id' => $sender, 'receiver_id' => $receiver, 'message_content' => $message->message)); $response = new Phalcon\Http\Response(); if ($status->success() == true) { $phql = "SELECT * \n \t\t\tFROM Messages \n \t\t\tWHERE message_id = :message_id:"; $message = $app->modelsManager->executeQuery($phql, array('message_id' => $status->getModel()->message_id))->getFirst(); $response->setStatusCode(200, "Created"); $response->setJsonContent(array('status' => "OK", 'data' => $message)); } else { $response->setStatusCode(400, "Conflict"); $errors = array(); foreach ($status->getMessages() as $errorMessage) { $errors[] = $errorMessage->getMessage(); } $response->setJsonContent(array('status' => 'ERROR', 'messages' => $errors)); } return $response; }); // Handles the cases where a non-existing resource is called. $app->notFound(function () use($app) { $app->response->setStatusCode(404, "Not Found")->sendHeaders(); echo "You've just typed me, and this is crazy, but this page was not found, so it doesn't exist maybe!"; }); // Starts the application.
include APP_PATH . "/app/config/loader.php"; include APP_PATH . "/app/config/services.php"; include APP_PATH . "/app/common/common.php"; /** * Handle the request */ $app = new \Phalcon\Mvc\Micro(); $app->setDI($di); $app->setEventsManager($eventManager); $app->response->setContentType('text/json', 'UTF-8'); $app->response->sendHeaders(); /** * 路由 */ //注册 $app->post('/sendcode', [$app->auth, 'getYZM']); $app->post('/checkphone', [$app->auth, 'checkphone']); $app->post('/headerimg', [$app->auth, 'uploadHeaderImg']); $app->post('/register', [$app->auth, 'register']); //登陆 $app->post('/login', [$app->auth, "login"]); //登出 $app->get('/logout', [$app->auth, "logout"]); //更新所在GPS $app->post('/addr', [$app->auth, "setAddr"]); //更新交友状态 $app->post('/actstatus', [$app->auth, "setActStatus"]); //设置关注 $app->post('/like', [new SnsController(), "setLikeAction"]); //获取我关注喜欢 $app->get('/like', [new SnsController(), "getLikeAction"]);
/** * Tests the notFound * * @issue T169 * @author Nikos Dimopoulos <*****@*****.**> * @since 2012-11-06 */ public function testMicroNotFound_T169() { $handler = new RestHandler($this); $app = new \Phalcon\Mvc\Micro(); $app->get('/api/site', array($handler, 'find')); $app->post('/api/site/save', array($handler, 'save')); $flag = false; $app->notFound(function () use(&$flag) { $flag = true; }); $_SERVER['REQUEST_METHOD'] = 'GET'; $_GET['_url'] = '/fourohfour'; $app->handle(); $this->assertTrue($flag); }
$phql = "SELECT * FROM Tasks"; $tasks = $app->modelsManager->executeQuery($phql); $data = new StdClass(); $data->tasks = []; foreach ($tasks as $task) { $data->tasks[] = ['id' => $task->id, 'name' => $task->name, 'shortDescription' => $task->short_description, 'longDescription' => $task->long_description, 'day' => $task->day, 'started' => $task->started, 'completed' => $task->completed]; } echo json_encode($data); }); $app->post('/tasks', function () use($app) { $request = $app->request->getJsonRawBody()->task; $name = $request->name; $short = $request->shortDescription; $long = $request->longDescription; $day = $request->day; $phql = "INSERT INTO Tasks(name, short_description, long_description, day, started, completed) VALUES(:name:, :short:, :long:, :day:, null, null)"; $status = $app->modelsManager->executeQuery($phql, ['name' => $name, 'short' => $short, 'long' => $long, 'day' => $day]); $response = new \Phalcon\Http\Response(); if ($status->success()) { $response->setJsonContent(['status' => 'OK']); } }); $app->put('/tasks/{id:[0-9]+}', function ($id) use($app) { $task = $app->request->getJsonRawBody()->task; $phql = "UPDATE Tasks SET name = :name:, short_description = :short:, long_description = :long:, started = :start:, completed = :complete: WHERE id = :id:"; $status = $app->modelsManager->executeQuery($phql, ['name' => $task->name, 'short' => $task->shortDescription, 'long' => $task->longDescription, 'start' => $task->started, 'complete' => $task->completed, 'id' => $id]); $response = new \Phalcon\Http\Response(); if ($status->success()) { $response->setJsonContent(['status' => 'OK']); } });
<?php use app\controllers\AuthController, app\models\feeds, app\models\feedcomments, app\models\friends, app\models\feedfollows, app\models\metafriends, app\models\notifications, app\models\profiles, app\models\users, app\models\privacy, app\models\embeds, app\models\photos, app\models\photoalbums, app\models\photocomments, app\models\phototaggroups, app\controllers\NotificationController, app\library\session\userSessions, app\library\fs\fileStorage as fs; /* * Micro doesn't support any headers (ex: cookies and redirection) */ $app = new \Phalcon\Mvc\Micro($di); $app->get('/ajax/test', function () { return 'Powered By Phalcon API'; }); $app->post('/ajax/test', function () { return 'Powered By Phalcon API'; }); $app->delete('/ajax/test', function () { return 'Powered By Phalcon API'; }); $app->post('/ajax/recover', function () use($app) { $mongo = $app->getDI()->getShared('mongo'); $json = $app->request->getJsonRawBody(); $user = $mongo->users->find(array('email' => $json->email), array('email' => 1)); $user = iterator_to_array($user, false); if (count($user) > 0) { $app->response->setJsonContent(array('success' => true)); } else { $app->response->setJsonContent(array('success' => false, 'message' => "Email doesn't exist.")); } $app->response->send(); }); $app->put('/ajax/recover/change', function () use($app) { $mongo = $app->getDI()->getShared('mongo'); });
<?php $app = new Phalcon\Mvc\Micro(); //Retrieves all robots $app->get('/api/robots', function () { }); //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();
$loader = new \Phalcon\Loader(); $loader->registerNamespaces(['Example\\Models' => __DIR__ . '/../app/models', 'DataTables' => __DIR__ . '/../../src/']); $loader->register(); $di = new \Phalcon\DI\FactoryDefault(); $di->setShared('db', function () { return new \Phalcon\Db\Adapter\Pdo\Sqlite(['dbname' => __DIR__ . '/../db.sqlite']); }); $di->setShared('view', function () { $view = new \Phalcon\Mvc\View(); $view->setViewsDir(__DIR__ . '/../app/views/'); $view->registerEngines(['.volt' => 'Phalcon\\Mvc\\View\\Engine\\Volt']); return $view; }); $app = new \Phalcon\Mvc\Micro($di); /** @noinspection PhpUndefinedMethodInspection */ $app->getRouter()->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI); $app->get('/', function () use($app) { $app['view']->render('index.volt', []); }); $app->post('/example_querybuilder', function () use($app) { $builder = $app->getService('modelsManager')->createBuilder()->columns('id, name, email, balance')->from('Example\\Models\\User'); $dataTables = new \DataTables\DataTable(); $dataTables->fromBuilder($builder)->sendResponse(); }); $app->get('/example_resultset', function () use($app) { $resultset = $app->getService('modelsManager')->createQuery("SELECT * FROM \\Example\\Models\\User")->execute(); $dataTables = new \DataTables\DataTable(); $dataTables->fromResultSet($resultset)->sendResponse(); }); $app->handle();
<?php $app = new Phalcon\Mvc\Micro(); $app->get('/', function () { echo "<h1>Welcome!</h1>"; }); $app->get('/say/hello/{name}', function ($name) use($app) { echo "<h1>Hello! {$name}</h1>"; echo "Your IP Address is ", $app->request->getClientAddress(); }); $app->post('/store/something', function () use($app) { $name = $app->request->getPost('name'); echo "<h1>Hello! {$name}</h1>"; }); $app->notFound(function () use($app) { $app->response->setStatusCode(404, "Not Found")->sendHeaders(); echo 'This is crazy, but this page was not found!'; }); $app->handle();
*/ $app = new Phalcon\Mvc\Micro($di); // Matches any route starting with i $app->post('/(i[a-z\\.]+)', function () use($app) { $response = new Phalcon\Http\Response(); // collect json data $rawpost = $app->di->get('rawpost'); $rawpost->setRawpost($app->request->getJsonRawBody(true)); // set up storage service $poststore = $app->di->get('poststore'); $poststore->setRawPost($rawpost); // make sure post is valid if (!$poststore->isValid()) { $response->setStatusCode(400, $poststore->getError()); $response->send(); die; } // store the posts if (!$poststore->storePosts()) { // log internal error message $response->setStatusCode(500, "Internal Error"); $response->send(); die; } // success $response->setStatusCode(200, "Success"); $response->send(); die; }); // view the request log $app->get('/requests', function () use($app) { $redis = $app->di->get('redis');