-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·55 lines (46 loc) · 1.67 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
<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require 'vendor/autoload.php';
$app = new \Slim\App;
// Get container
$container = $app->getContainer();
// Register component on container
$container['view'] = function ($container) {
$view = new \Slim\Views\Twig('twig', [
'cache' => FALSE
]);
$view->addExtension(new \Slim\Views\TwigExtension(
$container['router'],
$container['request']->getUri()
));
return $view;
};
// Render Twig template in route
$app->get('/', function ($request, $response, $args) {
return $this->view->render($response, 'home.html', [
'title' => 'T-rimmer'
]);
})->setName('home');
$app->post('/trim', function (Request $request, Response $response) {
$newResponse = $response->withHeader('Content-type', 'application/json');
$data = $request->getParsedBody();
$data['string'] = trim($data['string']);
$newResponse->getBody()->write(json_encode($data));
return $newResponse;
});
$app->post('/trim/right', function (Request $request, Response $response) {
$newResponse = $response->withHeader('Content-type', 'application/json');
$data = $request->getParsedBody();
$data['string'] = rtrim($data['string']);
$newResponse->getBody()->write(json_encode($data));
return $newResponse;
});
$app->post('/trim/left', function (Request $request, Response $response) {
$newResponse = $response->withHeader('Content-type', 'application/json');
$data = $request->getParsedBody();
$data['string'] = ltrim($data['string']);
$newResponse->getBody()->write(json_encode($data));
return $newResponse;
});
$app->run();