1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
|
8
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
9
|
|
|
// use Anax\Route\Exception\NotFoundException; |
10
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A sample JSON controller to show how a controller class can be implemented. |
14
|
|
|
* The controller will be injected with $di if implementing the interface |
15
|
|
|
* ContainerInjectableInterface, like this sample class does. |
16
|
|
|
* The controller is mounted on a particular route and can then handle all |
17
|
|
|
* requests for that mount point. |
18
|
|
|
*/ |
19
|
|
|
class WeatherCheckRestController implements ContainerInjectableInterface |
20
|
|
|
{ |
21
|
|
|
use ContainerInjectableTrait; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string $db a sample member variable that gets initialised |
27
|
|
|
*/ |
28
|
|
|
private $db = "not active"; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The initialize method is optional and will always be called before the |
33
|
|
|
* target method/action. This is a convienient method where you could |
34
|
|
|
* setup internal properties that are commonly used by several methods. |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
4 |
|
public function initialize() : void |
39
|
|
|
{ |
40
|
|
|
// Use to initialise member variables. |
41
|
4 |
|
$this->db = "active"; |
42
|
4 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* This is the index method action, it handles: |
46
|
|
|
* GET METHOD mountpoint |
47
|
|
|
* GET METHOD mountpoint/ |
48
|
|
|
* GET METHOD mountpoint/index |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
1 |
|
public function indexActionGet() : object |
53
|
|
|
{ |
54
|
1 |
|
return $this->di->get("page") |
55
|
1 |
|
->add("weather_check_rest") |
56
|
1 |
|
->render(["title" => "Weather Check"]); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function indexActionPost() |
60
|
|
|
{ |
61
|
1 |
|
$ip = $this->di->get("request")->getPost("ip") ?? null; |
62
|
1 |
|
$lat = $this->di->get("request")->getPost("lat") ?? null; |
63
|
1 |
|
$lon = $this->di->get("request")->getPost("lon") ?? null; |
64
|
1 |
|
$service = $this->di->get("weatherservice"); |
65
|
|
|
$data = [ |
66
|
1 |
|
"content" => $service->getWeather($ip, $lat, $lon), |
67
|
|
|
]; |
68
|
|
|
|
69
|
1 |
|
return [$data]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* This sample method dumps the content of $di. |
74
|
|
|
* GET mountpoint/dump-app |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
1 |
|
public function dumpDiActionGet() : array |
79
|
|
|
{ |
80
|
|
|
// Deal with the action and return a response. |
81
|
1 |
|
$services = implode(", ", $this->di->getServices()); |
82
|
|
|
$json = [ |
83
|
1 |
|
"message" => __METHOD__ . "<p>\$di contains: $services", |
84
|
1 |
|
"di" => $this->di->getServices(), |
85
|
|
|
]; |
86
|
1 |
|
return [$json]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Try to access a forbidden resource. |
93
|
|
|
* ANY mountpoint/forbidden |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
1 |
|
public function forbiddenAction() : array |
98
|
|
|
{ |
99
|
|
|
// Deal with the action and return a response. |
100
|
|
|
$json = [ |
101
|
1 |
|
"message" => __METHOD__ . ", forbidden to access.", |
102
|
|
|
]; |
103
|
1 |
|
return [$json, 403]; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|