|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Alexander Torosh <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Api; |
|
7
|
|
|
|
|
8
|
|
|
use Api\Controllers\AuthController; |
|
9
|
|
|
use Api\Controllers\IndexController; |
|
10
|
|
|
use Api\Controllers\UsersController; |
|
11
|
|
|
use Api\Exception\NotFoundException; |
|
12
|
|
|
use Phalcon\Mvc\Micro; |
|
13
|
|
|
use Phalcon\Mvc\Micro\Collection as MicroCollection; |
|
14
|
|
|
|
|
15
|
|
|
class Router |
|
16
|
|
|
{ |
|
17
|
|
|
public function init(Micro $app) |
|
18
|
|
|
{ |
|
19
|
|
|
// Not Found |
|
20
|
|
|
$app = $this->handleNotFound($app); |
|
21
|
|
|
|
|
22
|
|
|
// Mount Routes |
|
23
|
|
|
$app->mount($this->index()); |
|
24
|
|
|
$app->mount($this->users()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
private function handleNotFound(Micro $app): Micro |
|
28
|
|
|
{ |
|
29
|
|
|
// Not Found |
|
30
|
|
|
$app->notFound( |
|
31
|
|
|
function () use ($app) { |
|
32
|
|
|
throw new NotFoundException('Page Not Found', 404); |
|
33
|
|
|
} |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
return $app; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function index(): MicroCollection |
|
40
|
|
|
{ |
|
41
|
|
|
$collection = new MicroCollection(); |
|
42
|
|
|
$collection->setHandler(IndexController::class, true); |
|
43
|
|
|
$collection->setPrefix('/api'); |
|
44
|
|
|
|
|
45
|
|
|
$collection->get('', 'index'); |
|
46
|
|
|
|
|
47
|
|
|
return $collection; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function auth(): MicroCollection |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$collection = new MicroCollection(); |
|
53
|
|
|
$collection->setHandler(AuthController::class, true); |
|
54
|
|
|
$collection->setPrefix('/api/auth'); |
|
55
|
|
|
|
|
56
|
|
|
$collection->post('', 'authenticate'); |
|
57
|
|
|
|
|
58
|
|
|
return $collection; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function users(): MicroCollection |
|
62
|
|
|
{ |
|
63
|
|
|
$collection = new MicroCollection(); |
|
64
|
|
|
$collection->setHandler(UsersController::class, true); |
|
65
|
|
|
$collection->setPrefix('/api/users'); |
|
66
|
|
|
|
|
67
|
|
|
$collection->get('', 'usersList'); |
|
68
|
|
|
$collection->post('', 'create'); |
|
69
|
|
|
$collection->get('/{userID:\d+}', 'retrieve'); |
|
70
|
|
|
|
|
71
|
|
|
return $collection; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|