├── .htaccess ├── api └── posts.php ├── classes └── Database.php ├── database.sql └── index.php /.htaccess: -------------------------------------------------------------------------------- 1 | 2 | RewriteEngine On 3 | 4 | # Send would-be 404 requests to Craft 5 | RewriteCond %{REQUEST_FILENAME} !-f 6 | RewriteCond %{REQUEST_FILENAME} !-d 7 | RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC] 8 | RewriteRule (.+) index.php?p=$1 [QSA,L] 9 | -------------------------------------------------------------------------------- /api/posts.php: -------------------------------------------------------------------------------- 1 | $id)); 6 | if ($data != null) { 7 | echo json_encode($data[0]); 8 | } else { 9 | echo json_encode(['message' => 'Post Not Found.']); 10 | } 11 | } else { 12 | $data = DB::query("SELECT * FROM $tableName"); 13 | echo json_encode($data); 14 | } 15 | } elseif ($method == 'POST') { 16 | if ($_POST != null && !$id) { 17 | extract($_POST); 18 | DB::query("INSERT INTO $tableName VALUES(null, :title, :body, :author, null)", array(':title' => $title, ':body' => $body, ':author' => $author)); 19 | $data = DB::query("SELECT * FROM $tableName ORDER BY id DESC LIMIT 1"); 20 | echo json_encode(['message' => 'Post added to the database successfully.', 'success' => true, 'post' => $data[0]]); 21 | } else { 22 | echo json_encode(['message' => 'Please pill in all the credentials', 'success' => false]); 23 | } 24 | } elseif ($id) { 25 | $post = DB::query("SELECT * FROM $tableName WHERE id=:id", array(':id' => $id)); 26 | if ($post != null) { 27 | if ($method == 'PUT') { 28 | extract(json_decode(file_get_contents('php://input'), true)); 29 | // Update the Post in the Database 30 | DB::query("UPDATE $tableName SET title=:title, body=:body, author=:author WHERE id = :id", array(':title' => $title, ':body' => $body, ':author' => $author, ':id' => $id)); 31 | $data = DB::query("SELECT * FROM $tableName WHERE id=:id", array(':id' => $id)); 32 | echo json_encode(['post' => $data[0], 'message' => 'Post Updated successfully', 'success' => true]); 33 | } elseif ($method == 'DELETE') { 34 | DB::query("DELETE FROM $tableName WHERE id=:id", array(':id' => $id)); 35 | echo json_encode(['message' => 'Post Deleted successfully', 'success' => true]); 36 | } 37 | } else { 38 | echo json_encode(['message' => 'Post not found.', 'success' => false]); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /classes/Database.php: -------------------------------------------------------------------------------- 1 | setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 9 | 10 | return $pdo; 11 | } 12 | 13 | public static function query($query, $params = array()) 14 | { 15 | $stmt = self::connect()->prepare($query); 16 | $stmt->execute($params); 17 | if (explode(' ', $query)[0] == 'SELECT') { 18 | $data = $stmt->fetchAll(PDO::FETCH_ASSOC); 19 | 20 | return $data; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /database.sql: -------------------------------------------------------------------------------- 1 | -- phpMyAdmin SQL Dump 2 | -- version 4.8.3 3 | -- https://www.phpmyadmin.net/ 4 | -- 5 | -- Host: localhost:8889 6 | -- Generation Time: Feb 16, 2019 at 03:40 PM 7 | -- Server version: 5.7.23 8 | -- PHP Version: 7.2.10 9 | 10 | SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 11 | SET time_zone = "+00:00"; 12 | 13 | -- 14 | -- Database: `rest_api_php` 15 | -- 16 | 17 | -- -------------------------------------------------------- 18 | 19 | -- 20 | -- Table structure for table `posts` 21 | -- 22 | 23 | CREATE TABLE `posts` ( 24 | `id` int(11) NOT NULL, 25 | `title` varchar(100) NOT NULL, 26 | `body` text NOT NULL, 27 | `author` varchar(80) NOT NULL, 28 | `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP 29 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 30 | 31 | -- 32 | -- Dumping data for table `posts` 33 | -- 34 | 35 | INSERT INTO `posts` (`id`, `title`, `body`, `author`, `created_at`) VALUES 36 | (3, 'Post 3', 'This is the body for the post 3', 'Codebook', '2019-02-16 11:04:46'); 37 | 38 | -- 39 | -- Indexes for dumped tables 40 | -- 41 | 42 | -- 43 | -- Indexes for table `posts` 44 | -- 45 | ALTER TABLE `posts` 46 | ADD PRIMARY KEY (`id`); 47 | 48 | -- 49 | -- AUTO_INCREMENT for dumped tables 50 | -- 51 | 52 | -- 53 | -- AUTO_INCREMENT for table `posts` 54 | -- 55 | ALTER TABLE `posts` 56 | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4; 57 | -------------------------------------------------------------------------------- /index.php: -------------------------------------------------------------------------------- 1 | 'Table does not exists']); 31 | } 32 | --------------------------------------------------------------------------------