Last active
June 25, 2017 19:04
-
-
Save alphanetEX/5de8aa5902181da1a5e87af527f634ad to your computer and use it in GitHub Desktop.
CRUD SLIM EXAMPLE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
$app = new \Slim\Slim(); | |
$db = new mysqli("localhost", "name_of_user", "name_of_passwd", "name_of_db"); | |
$app->get("/producto", function() use($db, $app) { | |
// sleep(3); | |
$query = $db->query("SELECT * FROM productos ORDER BY id_producto DESC;"); | |
$productos = array(); | |
while ($fila = $query->fetch_assoc()) { | |
$productos[] = $fila; | |
} | |
$result = array("status" => "success", | |
"data" => $productos); | |
echo json_encode($result); | |
}); | |
$app->get("/producto/:id", function($id) use($db, $app) { | |
// sleep(3); | |
$query = $db->query("SELECT * FROM productos WHERE id_producto = $id;"); | |
$productos = $query->fetch_assoc(); | |
if ($query->num_rows == 1) { | |
$result = array("status" => "success", | |
"data" => $productos); | |
} else { | |
$result = array( | |
"status" => "error", | |
"message" => "El producto no existe"); | |
} | |
echo json_encode($result); | |
}); | |
$app->get("/random-productos", function() use($db, $app) { | |
// sleep(3); | |
$query = $db->query("SELECT * FROM productos ORDER BY RAND() LIMIT 1;"); | |
$productos = $query->fetch_assoc(); | |
if ($query->num_rows == 1) { | |
$result = array("status" => "success", | |
"data" => $productos); | |
} else { | |
$result = array( | |
"status" => "error", | |
"message" => "El productos no existe"); | |
} | |
echo json_encode($result); | |
}); | |
$app->post("/productos", function() use($db, $app) { | |
$json = $app->request->post("json"); | |
$query = "INSERT INTO productos VALUES(NULL," | |
. "'{$app->request->post("nombre")}'," | |
. "'{$app->request->post("precio")}', " | |
. "'{$app->request->post("descripcion")}'" | |
. ")"; | |
$insert = $db->query($query); | |
if ($insert) { | |
$result = array("status" => "success", | |
"message" => "Restaurante creado correctamente!!!"); | |
} else { | |
$result = array("status" => "error", "message" => "El producto NO SE HA creado!!!"); | |
} | |
echo json_encode($result); | |
}); | |
//PUT para ACTUALIZAR | |
$app->put('/productos/:id', function ($id) use($db, $app) { | |
$request = $app->request; | |
$sql = "UPDATE productos SET | |
nombre ='{$request->params("nombre")}', | |
precio ='{$request->params("precio")}', | |
vlan ='{$request->params("vlan")}', | |
test ='{$request->params("test")}' | |
WHERE id_producto=$id"; | |
$update = $db->query($sql); | |
if ($update) { | |
$result = array("status" => "true", "message" => "producto modificado correctamente"); | |
} else { | |
$result = array("status" => "false", "message" => "producto NO modificado"); | |
} | |
echo json_encode($result); | |
}); | |
$app->get("/delete-producto/:id", function($id) use($db, $app) { | |
$query = "DELETE FROM productos WHERE id_producto = {$id}"; | |
$delete = $db->query($query); | |
if ($delete) { | |
$result = array("status" => "success", "message" => "El producto se ha borrado correctamente!!!"); | |
} else { | |
$result = array("status" => "error", "message" => "El producto NO SE HA borrado!!!"); | |
} | |
echo json_encode($result); | |
}); | |
$app->post("/upload-file", function() use($db, $app) { | |
$result = array("status" => "error", "message" => "The file could not be uploaded"); | |
if (isset($_FILES["uploads"])) { | |
$piramideUploader = new PiramideUploader(); | |
$upload = $piramideUploader->upload("image", "uploads", "uploads", array("image/jpeg", "image/png", "image/gif")); | |
$file = $piramideUploader->getInfoFile(); | |
$file_name = $file["complete_name"]; | |
if (isset($upload) && $upload["uploaded"] == false) { | |
$result = array("status" => "error", | |
"message" => $upload["error"]); | |
} else { | |
$result = array("status" => "success", | |
"message" => "Fichero subido correctamente", | |
"filename"=>$file_name); | |
} | |
} | |
echo json_encode($result); | |
}); | |
$app->run(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment