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
|
<?php
namespace App\http\Controller;
use App\DB;
use App\View;
use App\http\Router;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class User
{
#[Route(path: '/account', methods: ['GET'])]
public function account(Request $request): Response
{
$user = DB::query('select username,email from users where id=:id', ['id' => $_SESSION['user']['id']])->fetch();
return new Response(View::render('account.twig', [
'user' => $user,
]));
}
#[Route(path: '/account', methods: ['POST'])]
public function accountSave(Request $request): Response
{
$username = $request->get('username');
$email = $request->get('email');
if ($request->get('password')) {
$password = password_hash($request->get('password'), PASSWORD_DEFAULT);
DB::query(
'update users set username=:username, email=:email, password=:password where id=:id',
[
'username' => $username,
'email' => $email,
'password' => $password,
'id' => $_SESSION['user']['id'],
]
);
} else {
DB::query(
'update users set username=:username, email=:email where id=:id',
[
'username' => $username,
'email' => $email,
'id' => $_SESSION['user']['id'],
]
);
}
$_SESSION['user']['username'] = $request->get('username');
return new RedirectResponse(Router::generate('user.account'));
}
}
|