-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
161 lines (140 loc) · 4.22 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\View\Engine\Volt;
use Phalcon\Mvc\Application;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter;
use Phalcon\Session\Adapter\Files as Session;
use Phalcon\Flash\Direct as FlashDirect;
use Phalcon\Flash\Session as FlashSession;
try {
// Register an autoloader
$loader = new Loader();
$loader->registerDirs(array(
'app/controllers/',
'app/controllers/admin/',
'app/models/'
));
$loader->register();
// Create a DI
$di = new FactoryDefault();
define('APPLICATION_PATH', __DIR__);
// Load the general configuration
$config = new \Phalcon\Config\Adapter\Ini('app/config/testing.ini');
$di->set('config', $config);
// setting up the database using the configuration
$di->set('db', function () use ($config) {
return new DbAdapter(array(
"host" => $config->database->host,
"username" => $config->database->username,
"password" => $config->database->password,
"dbname" => $config->database->dbname,
"port" => $config->database->port,
"charset" => "utf8",
));
});
// Set up the flash service
$di->set(
"flashDirect",
function () {
return new FlashDirect(
[
"error" => "alert alert-danger",
"success" => "alert alert-success",
"notice" => "alert alert-info",
"warning" => "alert alert-warning",
]);
}
);
// Set up the flash session service
$di->set(
"flash",
function () {
return new FlashSession(
[
"error" => "alert alert-danger",
"success" => "alert alert-success",
"notice" => "alert alert-info",
"warning" => "alert alert-warning",
]);
}
);
// Register Volt as a service
$di->set('voltService', function ($view, $di) {
$volt = new Volt($view, $di);
$volt->setOptions(
array(
"compiledPath" => "app/views.cache/",
"compiledExtension" => ".compiled",
'compileAlways' => true
)
);
$compiler = $volt->getCompiler();
$compiler->addFunction('strtotime', 'strtotime');
$compiler->addFilter("remove_dashes", function ($resolvedArgs) {
return "str_replace('-', ' ', $resolvedArgs)";
});
return $volt;
});
// Register Volt as template engine
$di->set('view', function () {
$view = new View();
$view->setViewsDir('app/views/');
$view->registerEngines(
array(
".volt" => 'voltService'
)
);
return $view;
});
// Start the session the first time when some component request the session service
$di->setShared('session', function () {
$session = new Session();
$session->start();
return $session;
});
ini_set('date.timezone', 'Europe/Sofia');
date_default_timezone_set('Europe/Sofia');
$di->getShared('db')->query('SET time_zone = "+3:00"')->execute();
// Handle the request
$application = new Application($di);
// Define a route
$application->router->add(
"/admin/:controller/:action/:params",
[
"controller" => 1,
"action" => 2,
"params" => 3,
]
);
// Define a route
$application->router->add(
"/admin/:controller",
[
"controller" => 1
]
);
// Define a route
$application->router->add(
"/admin/:controller/",
[
"controller" => 1
]
);
// Define a route
$application->router->add(
"/admin",
[
"controller" => "objects"
]
);
echo $application->handle()->getContent();
} catch (\Exception $e) {
echo "PhalconException: ", $e->getMessage();
echo "<pre>";
print_r($e->getTraceAsString());
echo "</pre>";
var_dump($e->getTrace());
}