-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
39 lines (33 loc) · 1.15 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
<?php
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
$twigPath = __DIR__ . '/templates';
$app = new Application();
$app->register(new TwigServiceProvider(), [
'twig.path' => $twigPath,
'twig.options' => [
'cache' => __DIR__ . '/compilation_cache',
'debug' => true,
],
]);
$app['twig']->addExtension(new Twig_Extension_Debug());
// Find all index.html.twig files and create matching routes, e.g. ./templates/map/index.html.twig ⟼ /map/
$directory = new RecursiveDirectoryIterator($twigPath);
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '@^' . preg_quote($twigPath, '@') . '(/(?:.+/)?)index\.html\.twig$@i', RecursiveRegexIterator::GET_MATCH);
foreach ($regex as $match) {
$path = $match[1];
// Create route.
$app->get($path, function(Request $request) use($app) {
try {
// Find and render index.html.twig
$templatePath = $request->getPathInfo() . 'index.html.twig';
return $app['twig']->render($templatePath);
} catch (\Exception $e) {
return $e->getMessage();
}
});
}
$app->run();