I have put together the back-end (API) with the Slim framework (v3) and MySQL.
In index.php I have:
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require '../src/config/db.php';
$app = new \Slim\App;
// Todos Routes
require '../src/routes/todos.php';
$app->run();
In db.php I have:
class db{
// Properties
private $dbhost = 'localhost';
private $dbuser = 'root';
private $dbpass = '';
private $dbname = 'todoapp';
// Connect
public function connect(){
$mysql_connect_str = "mysql:host=$this->dbhost;dbname=$this->dbname";
$dbConnection = new PDO($mysql_connect_str, $this->dbuser, $this->dbpass);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbConnection;
}
}
In todos.php I have:
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
$app = new \Slim\App;
$app->options('/{routes:.+}', function ($request, $response, $args) {
return $response;
});
$app->add(function ($req, $res, $next) {
$response = $next($req, $res);
return $response
->withHeader('Access-Control-Allow-Origin', '*')
->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
});
// Get Todos
$app->get('/api/todos', function(Request $request, Response $response){
$sql = "SELECT * FROM todos";
try{
// Get DB Object
$db = new db();
// Connect
$db = $db->connect();
$stmt = $db->query($sql);
$todos = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($todos);
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
// Add Todo
$app->post('/api/todo/add', function(Request $request, Response $response){
$title = $request->getParam('title');
$completed = $request->getParam('completed');
$sql = "INSERT INTO todos (title, completed) VALUES (:title,:completed)";
try {
// Get DB Object
$db = new db();
// Connect
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':completed', $completed);
$stmt->execute();
echo '{"notice": {"text": "Todo Added"}';
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
// Update Todo
$app->put('/api/todo/update/{id}', function(Request $request, Response $response){
$id = $request->getAttribute('id');
$title = $request->getParam('title');
$completed = $request->getParam('completed');
$sql = "UPDATE todos SET
title = :title,
completed = :completed WHERE id = $id";
try{
// Get DB Object
$db = new db();
// Connect
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->bindParam(':title', $title);
$stmt->bindParam(':completed', $completed);
$stmt->execute();
echo '{"notice": {"text": "Todo Updated"}';
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
// Delete Todo
$app->delete('/api/todo/delete/{id}', function(Request $request, Response $response){
$id = $request->getAttribute('id');
$sql = "DELETE FROM todos WHERE id = $id";
try{
// Get DB Object
$db = new db();
// Connect
$db = $db->connect();
$stmt = $db->prepare($sql);
$stmt->execute();
$db = null;
echo '{"notice": {"text": "Todo Deleted"}';
} catch(PDOException $e){
echo '{"error": {"text": '.$e->getMessage().'}';
}
});
Questions/concerns:
Is the application well-structured or should I move the logic into controllers?
If I should move the logic into controllers, what would be the best approach to doing that?
Post scriptum
I have added the front-end of the application here for those that might be interested.
