Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use Phalcon\Mvc\Micro;
- use Phalcon\Http\Response;
- use Phalcon\Loader;
- use Phalcon\Di\FactoryDefault;
- use Phalcon\Db\Adapter\Pdo\Mysql as PdoMysql;
- $app = new Micro();
- // Use Loader() to autoload our model
- $loader = new Loader();
- $loader->registerDirs(
- array(
- __DIR__ . '/models/'
- )
- )->register();
- $di = new FactoryDefault();
- // Set up the database service
- $di->set('db', function () {
- return new PdoMysql(
- array(
- "host" => "localhost",
- "username" => "root",
- "password" => "",
- "dbname" => "robotics"
- )
- );
- });
- // Create and bind the DI to the application
- $app = new Micro($di);
- $app->get('/',function(){
- echo "Pagina non utilizzabile";
- });
- $app->get('/api',function(){
- echo "Pagina non utilizzabile";
- });
- // Retrieves all robots
- $app->get('/api/robots', function () use ($app) {
- $phql = "SELECT * FROM Robots ORDER BY name";
- $robots = $app->modelsManager->executeQuery($phql);
- $data = array();
- foreach ($robots as $robot) {
- $data[] = array(
- 'id' => $robot->id,
- 'name' => $robot->name
- );
- }
- echo json_encode($data);
- });
- // Searches for robots with $name in their name
- $app->get('/api/robots/search/{name}', function ($name) use ($app) {
- $phql = "SELECT * FROM Robots WHERE name LIKE :name: ORDER BY name";
- $robots = $app->modelsManager->executeQuery(
- $phql,
- array(
- 'name' => '%' . $name . '%'
- )
- );
- $data = array();
- foreach ($robots as $robot) {
- $data[] = array(
- 'id' => $robot->id,
- 'name' => $robot->name
- );
- }
- echo json_encode($data);
- });
- // Retrieves robots based on primary key
- $app->get('/api/robots/{id:[0-9]+}', function ($id) use ($app) {
- $phql = "SELECT * FROM Robots WHERE id = :id:";
- $robot = $app->modelsManager->executeQuery($phql, array(
- 'id' => $id
- ))->getFirst();
- // Create a response
- $response = new Response();
- if ($robot == false) {
- $response->setJsonContent(
- array(
- 'status' => 'NOT-FOUND'
- )
- );
- } else {
- $response->setJsonContent(
- array(
- 'status' => 'FOUND',
- 'data' => array(
- 'id' => $robot->id,
- 'name' => $robot->name
- )
- )
- );
- }
- return $response;
- });
- $app->post('/api/contact', function () use ($app) {
- echo "Success!";
- });
- // ----------------------------------- //
- // ----- Here the problems!!!!! ------ //
- // ----------------------------------- //
- // Adds a new robot
- $app->post('/api/robots', function () use ($app) {
- //$robot = $app->request->getJsonRawBody();
- $temp = file_get_contents('php://input');
- $robot = json_decode($temp);
- echo array_values($robot);
- $phql = "INSERT INTO Robots (name, type, year) VALUES (:name:, :type:, :year:)";
- $status = $app->modelsManager->executeQuery($phql, array(
- 'name' => $robot->name,
- 'type' => $robot->type,
- 'year' => $robot->year
- ));
- // Create a response
- $response = new Response();
- // Check if the insertion was successful
- if ($status->success() == true) {
- // Change the HTTP status
- $response->setStatusCode(201, "Created");
- $robot->id = $status->getModel()->id;
- $response->setJsonContent(
- array(
- 'status' => 'OK',
- 'data' => $robot
- )
- );
- } else {
- // Change the HTTP status
- $response->setStatusCode(409, "Conflict");
- // Send errors to the client
- $errors = array();
- foreach ($status->getMessages() as $message) {
- $errors[] = $message->getMessage();
- }
- $response->setJsonContent(
- array(
- 'status' => 'ERROR',
- 'messages' => $errors
- )
- );
- }
- return $response;
- });
- // Updates robots based on primary key
- $app->put('/api/robots/{id:[0-9]+}', function ($id) use ($app) {
- $robot = $app->request->getJsonRawBody();
- $phql = "UPDATE Robots SET name = :name:, type = :type:, year = :year: WHERE id = :id:";
- $status = $app->modelsManager->executeQuery($phql, array(
- 'id' => $id,
- 'name' => $robot->name,
- 'type' => $robot->type,
- 'year' => $robot->year
- ));
- // Create a response
- $response = new Response();
- // Check if the insertion was successful
- 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;
- });
- // 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->handle();
- ?>
Add Comment
Please, Sign In to add comment