1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| <?php declare(strict_types=1);
use App\Application\Actions\User\ListUsersAction; use App\Application\Actions\User\ViewUserAction; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\App; use Slim\Interfaces\RouteCollectorProxyInterface as Group;
return function (App $app) { $app->add(new Tuupola\Middleware\CorsMiddleware([ "origin" => ["https://goodsaem.github.io","goodsaem.ml","goodsaem.ml","localhost"], "methods" => ["GET", "POST", "PUT", "PATCH", "DELETE","OPTIONS"], "headers.allow" => [], "headers.expose" => [], "credentials" => true, "cache" => 0, ]));
$app->get('/', function (Request $request, Response $response) { $response->getBody()->write('Hello world!'); return $response; });
$app->group('/users', function (Group $group) { $group->get('', ListUsersAction::class); $group->get('/{id}', ViewUserAction::class); });
$app->get('/api/customers' , function(Request $request , Response $response){ $response->getBody()->write('CUSTOMERS!'); return $response; });
$app->get('/tasks', function (Request $request, Response $response) { $db = $this->get(PDO::class); $sth = $db->prepare("SELECT * FROM tasks ORDER BY task_id"); $sth->execute(); $data = $sth->fetchAll(PDO::FETCH_ASSOC); $payload = json_encode($data); $response->getBody()->write($payload); return $response->withHeader('Content-Type', 'application/json'); });
$app->post('/tasks', function (Request $request, Response $response) { $db = $this->get(PDO::class); $contents = json_decode(file_get_contents('php://input'), true);
foreach ($contents as $item) { $sth = $db->prepare(" INSERT INTO `tasks` ( `title`, `start_date`, `due_date`, `status`, `priority`, `description`, `created_at` ) VALUES ( :title, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, :status, :priority, :description, CURRENT_TIMESTAMP )");
$sth->bindParam(':title', $item['title']); $sth->bindParam(':status', $item['status']); $sth->bindParam(':priority', $item['priority']); $sth->bindParam(':description', $item['description']); $sth->execute(); }
$response->getBody()->write( json_encode($contents)); return $response->withHeader('Content-Type', 'application/json'); }); };
|