-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
85 lines (65 loc) · 2.51 KB
/
index.php
File metadata and controls
85 lines (65 loc) · 2.51 KB
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
<?php
// composer autolad
require 'vendor/autoload.php';
$mainConfig = @require 'config.inc.php';
if (!$mainConfig || empty($mainConfig)) {
die('Technical error'); // should throw an Exception and decide afterwards if to log it and/or what response should be given to the client
}
$dbConfig = $mainConfig['db']['default'];
//--------
// this should be in an external file, required on demand
Propel::init('src/models/build/conf/hwtest_iplesca-conf.php');
// add config.inc.php credentials
$propelConfig = Propel::getConfiguration();
$propelConfig['datasources'][$dbConfig['name']]['connection'] = array(
'dsn' => 'mysql:host=' . $dbConfig['host'] . ';dbname=' . $dbConfig['name'],
'user' => $dbConfig['username'],
'password' => $dbConfig['password']
);
Propel::setConfiguration($propelConfig);
//--------
$app = new Silex\Application();
//$app['debug'] = true;
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
// general shared service to generate a proper page object
$app['createPage'] = $app->protect(function ($page = 'main') use ($app) {
// minimum safety
$definedPages = array('main', 'users');
if (in_array(strtolower($page), $definedPages)) {
// give it a proper a class name
// NOTE: the class will be autoloaded by composer, if there is one defined :P
$page .= 'Page';
} else {
$page = 'Show404Page';
}
// Prepare the Twig template engine. This is not required in ajax mode
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/src/views',
));
// fire the page controller and inject the template engine
$pageController = new $page($app, $app['twig']);
// the page controller will know what to do
return $pageController;
});
// Routing for all pages
$app->get('/', function () use ($app) {
return $app['createPage']('Main')->render();
})->bind('homepage');
$app->get('/users', function ($id = 0) use ($app) {
return $app->redirect('/'); // no user, no page
});
$app->get('/users/{id}', function ($id) use ($app) {
$userPage = $app['createPage']('Users');
$userPage->info($id);
return $userPage->render();
})->assert('id', '\d+') // accept only digits
->bind('userspage');
// AJAX routing, if any
$app->get('/ajax/{controller}/{action}', function () use ($app) {
// TODO, if time
});
// ... and finally deal with errors
$app->error(function () use ($app) {
return $app['createPage']('Error')->render();
});
$app->run();