ylvarw /
module
| 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 SampleJsonController 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 | /** |
||
| 33 | * The initialize method is optional and will always be called before the |
||
| 34 | * target method/action. This is a convienient method where you could |
||
| 35 | * setup internal properties that are commonly used by several methods. |
||
| 36 | * |
||
| 37 | * @return void |
||
| 38 | */ |
||
| 39 | public function initialize() : void |
||
| 40 | { |
||
| 41 | // Use to initialise member variables. |
||
| 42 | $this->db = "active"; |
||
| 43 | } |
||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * This is the index method action, it handles: |
||
| 49 | * GET METHOD mountpoint |
||
| 50 | * GET METHOD mountpoint/ |
||
| 51 | * GET METHOD mountpoint/index |
||
| 52 | * |
||
| 53 | * @return array |
||
| 54 | */ |
||
| 55 | public function indexActionGet() : array |
||
| 56 | { |
||
| 57 | // Deal with the action and return a response. |
||
| 58 | $json = [ |
||
| 59 | "message" => __METHOD__ . ", \$db is {$this->db}", |
||
| 60 | ]; |
||
| 61 | return [$json]; |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * This sample method dumps the content of $di. |
||
| 68 | * GET mountpoint/dump-app |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function dumpDiActionGet() : array |
||
| 73 | { |
||
| 74 | // Deal with the action and return a response. |
||
| 75 | $services = implode(", ", $this->di->getServices()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 76 | $json = [ |
||
| 77 | "message" => __METHOD__ . "<p>\$di contains: $services", |
||
| 78 | "di" => $this->di->getServices(), |
||
| 79 | ]; |
||
| 80 | return [$json]; |
||
| 81 | } |
||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | /** |
||
| 86 | * Try to access a forbidden resource. |
||
| 87 | * ANY mountpoint/forbidden |
||
| 88 | * |
||
| 89 | * @return array |
||
| 90 | */ |
||
| 91 | public function forbiddenAction() : array |
||
| 92 | { |
||
| 93 | // Deal with the action and return a response. |
||
| 94 | $json = [ |
||
| 95 | "message" => __METHOD__ . ", forbidden to access.", |
||
| 96 | ]; |
||
| 97 | return [$json, 403]; |
||
| 98 | } |
||
| 99 | } |
||
| 100 |