1 | <?php |
||
2 | |||
3 | namespace App\Controller; |
||
4 | |||
5 | use Symfony\Component\HttpFoundation\Response; |
||
6 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
7 | use Symfony\Component\Routing\Annotation\Route; |
||
8 | |||
9 | class LuckyController |
||
10 | { |
||
11 | #[Route('/lucky/number')] |
||
12 | public function number(): Response |
||
13 | { |
||
14 | $number = random_int(0, 100); |
||
15 | |||
16 | return new Response( |
||
17 | '<html><body>Lucky number: '.$number.'</body></html>' |
||
18 | ); |
||
19 | } |
||
20 | |||
21 | #[Route("/lucky/hello")] |
||
22 | public function hello(): Response |
||
23 | { |
||
24 | return new Response( |
||
25 | '<html><body>Hi to you!</body></html>' |
||
26 | ); |
||
27 | } |
||
28 | |||
29 | #[Route("/api/lucky/number")] |
||
30 | public function jsonNnumber(): Response |
||
31 | { |
||
32 | $this->number = random_int(0, 100); |
||
33 | |||
34 | $data = [ |
||
35 | 'lucky-number' => $this->number, |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
36 | 'lucky-message' => 'Hi there!', |
||
37 | ]; |
||
38 | |||
39 | |||
40 | $response = new JsonResponse($data); |
||
41 | $response->setEncodingOptions( |
||
42 | $response->getEncodingOptions() | JSON_PRETTY_PRINT |
||
43 | ); |
||
44 | return $response; |
||
45 | |||
46 | // return new JsonResponse; |
||
47 | |||
48 | // $response = new Response(); |
||
49 | // $response->setContent(json_encode($data)); |
||
50 | // $response->headers->set('Content-Type', 'application/json'); |
||
51 | |||
52 | // return $response; |
||
53 | } |
||
54 | } |
||
55 |