|
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 LuckyControllerJson |
|
10
|
|
|
{ |
|
11
|
|
|
private $number; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @Route("/api/lucky/number") |
|
17
|
|
|
*/ |
|
18
|
|
|
public function number(): Response |
|
19
|
|
|
{ |
|
20
|
|
|
$this->number = random_int(0, 100); |
|
21
|
|
|
|
|
22
|
|
|
$data = [ |
|
23
|
|
|
'lucky-number' => $this->number |
|
|
|
|
|
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
$response = new Response(); |
|
27
|
|
|
$response->setContent(json_encode($data)); |
|
28
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
|
29
|
|
|
|
|
30
|
|
|
return $response; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @Route("/api/lucky/number2") |
|
37
|
|
|
*/ |
|
38
|
|
|
public function number2(): Response |
|
39
|
|
|
{ |
|
40
|
|
|
$this->number = random_int(0, 100); |
|
41
|
|
|
|
|
42
|
|
|
$data = [ |
|
43
|
|
|
'message' => 'Welcome to the lucky number API', |
|
44
|
|
|
'lucky-number' => $this->number |
|
|
|
|
|
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
//return new JsonResponse($data); |
|
48
|
|
|
|
|
49
|
|
|
$response = new JsonResponse($data); |
|
50
|
|
|
$response->setEncodingOptions( |
|
51
|
|
|
$response->getEncodingOptions() | JSON_PRETTY_PRINT |
|
52
|
|
|
); |
|
53
|
|
|
return $response; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @Route("/api/lucky/number/{min}/{max}") |
|
60
|
|
|
*/ |
|
61
|
|
|
public function number3(int $min, int $max): Response |
|
62
|
|
|
{ |
|
63
|
|
|
$this->number = random_int($min, $max); |
|
64
|
|
|
|
|
65
|
|
|
$data = [ |
|
66
|
|
|
'message' => 'Welcome to the lucky number API', |
|
67
|
|
|
'min number' => $min, |
|
68
|
|
|
'max number' => $max, |
|
69
|
|
|
'lucky-number' => $this->number |
|
|
|
|
|
|
70
|
|
|
]; |
|
71
|
|
|
|
|
72
|
|
|
return new JsonResponse($data); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|