-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
74 lines (61 loc) · 1.94 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
<?php
require_once 'vendor/autoload.php';
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\App;
use Slim\Exception\NotFoundException;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
$app = new App();
$app->add(function (Request $request, Response $response, callable $next) {
$uri = $request->getUri();
$path = $uri->getPath();
if ($path != '/' && substr($path, -1) == '/') {
// permanently redirect paths with a trailing slash
// to their non-trailing counterpart
$uri = $uri->withPath(substr($path, 0, -1));
if ($request->getMethod() == 'GET') {
return $response->withRedirect((string)$uri, 301);
} else {
return $next($request->withUri($uri), $response);
}
}
return $next($request, $response);
});
$app->get('/', function (Request $request, Response $response, array $args) {
$response->getBody()->write(render('index'));
return $response;
});
$app->get('/base', function (Request $request, Response $response, array $args) {
throw new NotFoundException($request, $response);
});
$app->get('/{page}', function (Request $request, Response $response, array $args) {
$page = $args['page'];
$rendered = render($page);
if ($rendered !== null) {
$response->getBody()->write($rendered);
} else {
throw new NotFoundException($request, $response);
}
return $response;
});
$app->run();
/**
* @param string $page
* @param array $data
* @return string|null
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
function render(string $page, array $data = []): ?string
{
$loader = new Twig\Loader\FilesystemLoader('tpl');
$filePath = $page . '.twig';
if (!$loader->exists($filePath)) {
return null;
}
$twig = new Twig\Environment($loader);
return $twig->render($filePath, $data);
}