Web Page consists of a series of Resources
Example of simple HTTP request and response
GET / HTTP/1.1⏎
Host: mendelu.cz⏎
⏎
/
represents path from URL and mendelu.cz
is hostname
GET /welcome.php?name=John HTTP/1.1⏎
Host: domain.tld⏎
⏎
welcome.php:
<?php
echo "<!DOCTYPE HTML><html>";
echo "<body><h1>Hello" . $_GET['name'] . "</h1></body>";
echo "</html>";
POST /login.php?l=en HTTP/1.1⏎
Host: domain.tld⏎
Content-Type: application/x-www-form-urlencoded⏎
Content-Length: 48⏎
⏎
username=John&password=NotTooSecret&action=login
welcome.php:
<?php
echo "<!DOCTYPE HTML><html><body>";
if (($_POST['username'] == 'John') &&
($_POST['password'] == 'NotTooSecret')) {
if ($_GET['l'] == 'en') {
echo "<h1>Hello " . $_POST['username'] . "</h1>";
} else {
echo "<h1>Ciao " . $_POST['username'] . "</h1>";
}
}
echo "</body></html>";
<?php
$tpl = new Latte\Engine();
if (($_POST['username'] == 'John') &&
($_POST['password'] == 'NotTooSecret')) {
$tpl->render("welcome.latte", ['username' => $_POST['username'], 'language' => $_GET['l']]);
} else {
$tpl->render("login.latte", 'language' => $_GET['l']);
}
<?php
$tpl = new Latte\Engine();
if (($_POST['username'] == 'John') &&
($_POST['password'] == 'NotTooSecret')) {
$tplVars = [
'username' => $_POST['username'],
'language' => $_GET['l']
];
$tpl->render("welcome.latte", $tplVars);
} else {
$tplVars = ['language' => $_GET['l']];
$tpl->render("login.latte", $tplVars);
}
welcome.latte
<!DOCTYPE html>
<html>
<head></head>
<body>
{if $language == 'en'}
<h1>Hi {$username}</h1>
{else}
<h1>Ciao {$username}</h1>
{/if}
</body>
</html>
welcome.latte
{extends 'layout.latte'}
{block content}
{if $language == 'en'}
<h1>Hi {$username}</h1>
{else}
<h1>Ciao {$username}</h1>
{/if}
{/block}
layout.latte
<!DOCTYPE html>
<html>
<head></head>
<body>
{include content}
</body>
</html>
Real layout is slightly bigger!
HTTP request:
GET /welcome?name=John HTTP/1.1⏎
Host: domain.tld⏎
⏎
Requeste handler:
$app->get('/welcome', function (Request $request, Response $response) {
$tplVars = ['username' => $request->getQueryParam('name')];
return $this->view->render($response, 'welcome.latte', $tplVars);
);
$app->get('/welcome', function (Request $request, Response $response) {
$data = $request->getParsedBody();
if (($data['username'] == 'JohnDoe') &&
($data['password'] == 'NotSoSecret')) {
$tplVars = [
'username' => $data['username'],
'language' => $request->getQueryParam('l'),
];
return $this->view->render($response, 'welcome.latte', $tplVars);
} else {
$tplVars = [
'language' = $request->getQueryParam('l')
];
return $this->view->render($response, 'login.latte', $tplVars);
}
);