1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Controller; |
4
|
|
|
|
5
|
|
|
use Anax\Commons\ContainerInjectableInterface; |
6
|
|
|
use Anax\Commons\ContainerInjectableTrait; |
7
|
|
|
// use Anax\Route\Exception\ForbiddenException; |
8
|
|
|
// use Anax\Route\Exception\NotFoundException; |
9
|
|
|
// use Anax\Route\Exception\InternalErrorException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A sample controller to show how a controller class can be implemented. |
13
|
|
|
* The controller will be injected with $di if implementing the interface |
14
|
|
|
* ContainerInjectableInterface, like this sample class does. |
15
|
|
|
* The controller is mounted on a particular route and can then handle all |
16
|
|
|
* requests for that mount point. |
17
|
|
|
*/ |
18
|
|
|
class SampleController implements ContainerInjectableInterface |
19
|
|
|
{ |
20
|
|
|
use ContainerInjectableTrait; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string $db a sample member variable that gets initialised |
26
|
|
|
*/ |
27
|
|
|
private $db = "not active"; |
28
|
|
|
|
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
|
|
|
public function initialize() : void |
39
|
|
|
{ |
40
|
|
|
// Use to initialise member variables. |
41
|
|
|
$this->db = "active"; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* This is the index method action, it handles: |
48
|
|
|
* ANY METHOD mountpoint |
49
|
|
|
* ANY METHOD mountpoint/ |
50
|
|
|
* ANY METHOD mountpoint/index |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function indexAction() : string |
55
|
|
|
{ |
56
|
|
|
// Deal with the action and return a response. |
57
|
|
|
return __METHOD__ . ", \$db is {$this->db}"; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Add the request method to the method name to limit what request methods |
64
|
|
|
* the handler supports. |
65
|
|
|
* GET mountpoint/info |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function infoActionGet() : string |
70
|
|
|
{ |
71
|
|
|
// Deal with the action and return a response. |
72
|
|
|
return __METHOD__ . ", \$db is {$this->db}"; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* This sample method action it the handler for route: |
79
|
|
|
* GET mountpoint/create |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function createActionGet() : string |
84
|
|
|
{ |
85
|
|
|
// Deal with the action and return a response. |
86
|
|
|
return __METHOD__ . ", \$db is {$this->db}"; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* This sample method action it the handler for route: |
93
|
|
|
* POST mountpoint/create |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function createActionPost() : string |
98
|
|
|
{ |
99
|
|
|
// Deal with the action and return a response. |
100
|
|
|
return __METHOD__ . ", \$db is {$this->db}"; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* This sample method action takes one argument: |
107
|
|
|
* GET mountpoint/argument/<value> |
108
|
|
|
* |
109
|
|
|
* @param mixed $value |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function argumentActionGet($value) : string |
114
|
|
|
{ |
115
|
|
|
// Deal with the action and return a response. |
116
|
|
|
return __METHOD__ . ", \$db is {$this->db}, got argument '$value'"; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* This sample method action takes zero or one argument and you can use - as a separator which will then be removed: |
123
|
|
|
* GET mountpoint/defaultargument/ |
124
|
|
|
* GET mountpoint/defaultargument/<value> |
125
|
|
|
* GET mountpoint/default-argument/ |
126
|
|
|
* GET mountpoint/default-argument/<value> |
127
|
|
|
* |
128
|
|
|
* @param mixed $value with a default string. |
129
|
|
|
* |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function defaultArgumentActionGet($value = "default") : string |
133
|
|
|
{ |
134
|
|
|
// Deal with the action and return a response. |
135
|
|
|
return __METHOD__ . ", \$db is {$this->db}, got argument '$value'"; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* This sample method action takes two typed arguments: |
142
|
|
|
* GET mountpoint/typed-argument/<string>/<int> |
143
|
|
|
* |
144
|
|
|
* NOTE. Its recommended to not use int as type since it will still |
145
|
|
|
* accept numbers such as 2hundred givving a PHP NOTICE. So, its better to |
146
|
|
|
* deal with type check within the action method and throuw exceptions |
147
|
|
|
* when the expected type is not met. |
148
|
|
|
* |
149
|
|
|
* @param mixed $value with a default string. |
|
|
|
|
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function typedArgumentActionGet(string $str, int $int) : string |
154
|
|
|
{ |
155
|
|
|
// Deal with the action and return a response. |
156
|
|
|
return __METHOD__ . ", \$db is {$this->db}, got string argument '$str' and int argument '$int'."; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* This sample method action takes a variadic list of arguments: |
163
|
|
|
* GET mountpoint/variadic/ |
164
|
|
|
* GET mountpoint/variadic/<value> |
165
|
|
|
* GET mountpoint/variadic/<value>/<value> |
166
|
|
|
* GET mountpoint/variadic/<value>/<value>/<value> |
167
|
|
|
* etc. |
168
|
|
|
* |
169
|
|
|
* @param array $value as a variadic parameter. |
170
|
|
|
* |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
public function variadicActionGet(...$value) : string |
174
|
|
|
{ |
175
|
|
|
// Deal with the action and return a response. |
176
|
|
|
return __METHOD__ . ", \$db is {$this->db}, got '" . count($value) . "' arguments: " . implode(", ", $value); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Adding an optional catchAll() method will catch all actions sent to the |
183
|
|
|
* router. YOu can then reply with an actual response or return void to |
184
|
|
|
* allow for the router to move on to next handler. |
185
|
|
|
* A catchAll() handles the following, if a specific action method is not |
186
|
|
|
* created: |
187
|
|
|
* ANY METHOD mountpoint/** |
188
|
|
|
* |
189
|
|
|
* @param array $args as a variadic parameter. |
190
|
|
|
* |
191
|
|
|
* @return mixed |
192
|
|
|
*/ |
193
|
|
|
public function catchAll(...$args) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
// Deal with the request and send an actual response, or not. |
196
|
|
|
//return __METHOD__ . ", \$db is {$this->db}, got '" . count($args) . "' arguments: " . implode(", ", $args); |
197
|
|
|
return; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.