-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.pre
58 lines (41 loc) · 1.17 KB
/
server.pre
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
<?php
use Silex\Application;
use Silex\Provider\SessionServiceProvider;
use Symfony\Component\HttpFoundation\Request;
use App\Component\AddTask;
use App\Component\Page;
use App\Component\TaskList;
$app = new Application();
$app->register(new SessionServiceProvider());
$app->get("/", (Request $request) => {
$session = $request->getSession();
$tasks = $session->get("tasks", []);
return (
<Page>
<TaskList>{$tasks}</TaskList>
<AddTask></AddTask>
</Page>
);
});
$app->post("/add", (Request $request) => {
$session = $request->getSession();
$id = $session->get("id", 0);
$tasks = $session->get("tasks", []);
$tasks[] = [
"id" => $id++,
"text" => $request->get("text"),
];
$session->set("id", $id);
$session->set("tasks", $tasks);
return $app->redirect("/");
});
$app->get("/remove/{id}", (Request $request, $id) => {
$session = $request->getSession();
$tasks = $session->get("tasks", []);
$tasks = array_filter($tasks, ($task) => {
return $task["id"] !== (int) $id;
});
$session->set("tasks", $tasks);
return $app->redirect("/");
});
$app->run();