-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
74 lines (59 loc) · 1.82 KB
/
index.php
File metadata and controls
74 lines (59 loc) · 1.82 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
<?php
// composer require viewi/viewi:v2.x-dev
// local development server for testing
// Run:
// php -S localhost:8000 -t server/
// npm run --prefix server/viewi-app/js/ watch
// cd server/viewi-app/js/
// npm run watch
// for stubs
// // php -S localhost:8001 -t public/
use Components\Models\PostModel;
use Viewi\Components\Http\Message\Response;
require __DIR__ . '/../vendor/autoload.php';
// Viewi application here
/**
* @var Viewi\App
*/
$app = include __DIR__ . '/viewi-app/viewi.php';
// Demo API
$router = $app->router();
$router->get('/api/post/{id}', function (int $id) {
$post = new PostModel();
$post->id = $id;
$post->name = 'View Post Demo';
return $post;
});
$router->post('/api/session', function () {
return ['CSRFToken' => 'token'];
});
$router->post('/api/movies', function () {
return [
['id' => 1, 'name' => 'Inception', 'year' => 2010],
['id' => 2, 'name' => 'Interstellar', 'year' => 2014],
['id' => 3, 'name' => 'Dunkirk', 'year' => 2017],
];
});
$router->get('/api/movies', function () {
return [
['id' => 1, 'name' => 'Inception', 'year' => 2010],
['id' => 2, 'name' => 'Interstellar', 'year' => 2014],
['id' => 3, 'name' => 'Dunkirk', 'year' => 2017],
];
});
// Viewi components
include __DIR__ . '/viewi-app/routes.php';
$response = $app->run();
if (is_string($response)) {
header("Content-type: text/html; charset=utf-8");
echo $response;
} elseif ($response instanceof Response) {
http_response_code($response->status);
foreach ($response->headers as $name => $value) {
header("$name: $value");
}
echo $response->body;
} else {
header("Content-type: application/json; charset=utf-8");
echo json_encode($response);
}