-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·113 lines (96 loc) · 2.76 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* Created by IntelliJ IDEA.
* User: godsoul
* Date: 2016/1/10
* Time: 22:54
*/
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\Micro as Micro;
use \Phalcon\Loader as Loader;
use Phalcon\Db\Adapter\Pdo\Mysql as MysqlAdapter;
use Phalcon\Config\Adapter\Ini as ConfigIni;
use Phalcon\Mvc\Micro\Collection as MicroCollection;
// Setup loader
$loader = new Loader();
$loader->registerDirs(array(
__DIR__ . '/app/models/',
__DIR__ . '/app/controllers/',
__DIR__ . '/library/'
))->register();
// Read the configuration
$config = new ConfigIni(__DIR__ . '/config/config.ini');
//Start DI
$di = new FactoryDefault();
$di->set('redis',function(){
return new RedisTest();
},true);
// Start Micro
$app = new Micro();
$app->setDI($di);
// Setup the database service
$app['db'] = function() use ($config) {
return new MysqlAdapter(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->dbname,
"charset" => $config->database->charset
));
};
// Include controllers
$app['controllers'] = function() {
return [
'core' => true,
'user' => true,
'messages' => true
];
};
// Authentication
//$app['auth'] = function() use ($app, $config) {
// $auth = array();
// $authorization = $app->request->getHeader("AUTHORIZATION");
// if ($authorization) {
// $cut = str_replace('Basic ', '', $authorization);
// $creds = explode(':', base64_decode($cut));
// $auth['login'] = $creds[0];
// $auth['password'] = $creds[1];
// } else {
// $auth['login'] = null;
// $auth['password'] = null;
// }
//
// $usr = new Users();
// $auth['id'] = $usr->getUserId($auth['login'], $auth['password']);
//
// return $auth;
//};
// CoreController
if ($app['controllers']['core']) {
$core = new MicroCollection();
// Set the handler & prefix
$core->setHandler(new CoreController($app));
$core->setPrefix('/');
// Set routers
$core->get('/', 'index');
$app->mount($core);
}
// UsersController
if ($app['controllers']['user']) {
$users = new MicroCollection();
// Set the handler & prefix
$users->setHandler(new UserController($app));
$users->setPrefix('/user');
// Set routers
$users->post('/', 'create');
$users->put('/{id}', 'update');
$users->delete('/{id}', 'delete');
$users->get('/', 'userList');
$users->get('/{id}', 'info');
$app->mount($users);
}
// Not Found
$app->notFound(function () use ($app) {
$app->response->setStatusCode(404, "Not Found")->sendHeaders();
});
$app->handle();